Skip to main content

Data structure code challenge questions along with their solutions in PHP:

Data structure code challenge questions along with their solutions in PHP:

  1. Linked List Implementation

Question: Implement a singly linked list in PHP with methods for insertion at the beginning, insertion at the end, deletion from the beginning, deletion from the end, and traversal.

class Node { public $data; public $next; public function __construct($data) { $this->data = $data; $this->next = null; } } class LinkedList { private $head; public function __construct() { $this->head = null; } public function insertBeginning($data) { $newNode = new Node($data); $newNode->next = $this->head; $this->head = $newNode; } public function insertEnd($data) { $newNode = new Node($data); if ($this->head === null) { $this->head = $newNode; } else { $current = $this->head; while ($current->next !== null) { $current = $current->next; } $current->next = $newNode; } } public function deleteBeginning() { if ($this->head !== null) { $temp = $this->head; $this->head = $this->head->next; $temp = null; } } public function deleteEnd() { if ($this->head === null) { return; } if ($this->head->next === null) { $this->head = null; return; } $current = $this->head; while ($current->next->next !== null) { $current = $current->next; } $current->next = null; } public function display() { $current = $this->head; while ($current !== null) { echo $current->data . " "; $current = $current->next; } } } $list = new LinkedList(); $list->insertBeginning(3); $list->insertBeginning(2); $list->insertBeginning(1); $list->insertEnd(4); $list->insertEnd(5); $list->deleteBeginning(); $list->deleteEnd(); $list->display(); // Output: 2 3 4

  1. Stack Implementation

Question: Implement a stack using an array in PHP with functions for push, pop, and peek operations.

class Stack { private $stack; public function __construct() { $this->stack = []; } public function push($data) { array_push($this->stack, $data); } public function pop() { if ($this->isEmpty()) { return null; } return array_pop($this->stack); } public function peek() { if ($this->isEmpty()) { return null; } return end($this->stack); } public function isEmpty() { return empty($this->stack); } } $stack = new Stack(); $stack->push(1); $stack->push(2); $stack->push(3); echo $stack->pop(); // Output: 3 echo $stack->peek(); // Output: 2


  1. Queue Implementation

Question: Implement a queue using an array in PHP with functions for enqueue, dequeue, and peek operations.

class Queue { private $queue; public function __construct() { $this->queue = []; } public function enqueue($data) { array_push($this->queue, $data); } public function dequeue() { if ($this->isEmpty()) { return null; } return array_shift($this->queue); } public function peek() { if ($this->isEmpty()) { return null; } return $this->queue[0]; } public function isEmpty() { return empty($this->queue); } } $queue = new Queue(); $queue->enqueue(1); $queue->enqueue(2); $queue->enqueue(3); echo $queue->dequeue(); // Output: 1 echo $queue->peek(); // Output: 2


These examples cover some basic data structure implementations in PHP. You can expand upon them or create additional challenges based on these concepts. Let me know if you need more examples or have any other questions!

Comments

Popular posts from this blog

MySQL's ACID compliance

Mysql acid compliance ACID is an acronym that stands for four key properties of database transactions: Atomicity Ensures that a transaction is treated as a single, indivisible unit of work Either all operations within a transaction are completed successfully, or none are If any part of the transaction fails, the entire transaction is rolled back to its previous state Prevents partial updates that could leave the database in an inconsistent state Consistency Guarantees that a transaction brings the database from one valid state to another valid state All data written to the database must adhere to defined rules, constraints, cascades, triggers, and other database integrity mechanisms Ensures that any transaction will not break the database's predefined rules Isolation Determines how and when changes made by one transaction become visible to other transactions Prevents interference between concurrent transactions MySQL provides different isolation levels: Read Uncommitted Read Commit...

PHP OOPs exercise - Basic Oops

  Here are key PHP OOP (Object-Oriented Programming) exercise questions with solutions: Basic Class and Object Exercise: // Create a simple bank account class class BankAccount {     private $accountNumber;     private $balance;     public function __construct($accountNumber, $initialBalance = 0) {         $this->accountNumber = $accountNumber;         $this->balance = $initialBalance;     }     public function deposit($amount) {         if ($amount > 0) {             $this->balance += $amount;             return true;         }         return false;  ...

Interview questions for Senior PHP Developer particle41.com

1.Self Introduction 2.Basic questions on session and cookie. 3.Where is session stored? 4.Difference between Cookie and session. 5.Will there be any session before session start? 6.Post Max execution time.How can we modify it? 7.We have a string, "BJFSJK".Without any php function reverse it with half the string length.   To reverse the string with half the string length without using any PHP functions, you can implement a simple algorithm to achieve the desired result. Here's how you can do it: Initialize two pointers, one at the beginning of the string and the other at the midpoint of the string. Swap characters between these two pointers iteratively, moving the pointers towards each other until they meet or cross each other. Here's the PHP code to implement this algorithm:  <?php $string = "ABC100"; $length = strlen($string); // Calculate the midpoint of the string $midpoint = (int)($length / 2); // Initialize pointers $start = 0; $end = $length - 1; //...