DSA Stack Queue LinkedList Questions Sample
DSA Stack Queue LinkedList Questions Sample
Q1 [Easy] - Write pseudocode to implement a stack using an array. Identify any logical error in the push
operation.
Solution:
Pseudocode:
procedure PUSH(stack, item)
if top == size - 1 then
print 'Stack Overflow'
else
top = top + 1
stack[top] = item
Java Equivalent:
if(top == stack.length - 1) {
System.out.println("Overflow");
} else {
stack[++top] = item;
}
Q2 [Easy] - Fill in the blank: In a circular queue implemented with an array, the condition for queue full is
______.
Solution:
Solution: (rear + 1) % size == front
Q3 [Medium] - Detect the error in this singly linked list deletion pseudocode:
curr = head
prev = curr
curr = curr.next
prev.next = curr.next
Solution:
Error: prev is uninitialized if the key is at the head. Fix by checking if head.data ==
key before the loop.
Q4 [Medium] - Implement a deque using a doubly linked list. Provide Java code for insertFront().
Solution:
Java:
void insertFront(int key) {
Node newNode = new Node(key);
newNode.next = front;
Data Structures Practice Questions
Q5 [Hard] - Write pseudocode to reverse a stack using recursion only (no loop).
Solution:
Pseudocode:
procedure REVERSE(stack)
if stack is not empty then
temp = stack.pop()
REVERSE(stack)
insertAtBottom(stack, temp)