Bird
0
0
DSA Cprogramming~20 mins

Implement Stack Using Queue in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stack Using Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output after these stack operations?

Consider a stack implemented using two queues. The operations are:

  • push(1)
  • push(2)
  • pop()
  • push(3)
  • pop()

What is the printed output of the popped elements?

DSA C
/* Using two queues q1 and q2 to implement stack */
// push(x): enqueue x to q2, then enqueue all elements of q1 to q2, swap q1 and q2
// pop(): dequeue from q1

// Operations:
// push(1)
// push(2)
// pop() -> prints popped element
// push(3)
// pop() -> prints popped element
A[1, 2]
B[1, 3]
C[2, 1]
D[2, 3]
Attempts:
2 left
💡 Hint

Remember that the last pushed element is popped first in a stack.

🧠 Conceptual
intermediate
1:00remaining
Which queue operation is used to simulate stack's pop?

In a stack implemented using queues, which queue operation corresponds to the stack's pop operation?

AEnqueue (adding element to the back)
BDequeue (removing element from the front)
CPeek (viewing front element without removing)
DSize (counting elements in queue)
Attempts:
2 left
💡 Hint

Pop removes the top element from the stack.

🔧 Debug
advanced
2:00remaining
Find the error in this stack push implementation using one queue

Given this push function for stack using a single queue, what error will occur?

void push(int x) {
    q.push(x);
    for (int i = 0; i < q.size() - 1; i++) {
        int val = q.front();
        q.pop();
        q.push(val);
    }
}
ANo error, works correctly
BCompilation error due to missing queue declaration
CInfinite loop because q.size() changes inside loop
DRuntime error due to modifying queue size during loop
Attempts:
2 left
💡 Hint

The queue size remains constant during the loop because each pop is followed by a push.

Predict Output
advanced
2:00remaining
What is the stack content after these operations using one queue?

Using a stack implemented with one queue, perform these operations:

  • push(10)
  • push(20)
  • push(30)
  • pop()

What is the state of the queue representing the stack after these operations?

DSA C
/* push(x): enqueue x, then rotate queue to put x at front
   pop(): dequeue front element */
A30 -> 20 -> 10 -> null
B10 -> 20 -> null
C20 -> 10 -> null
D20 -> 30 -> 10 -> null
Attempts:
2 left
💡 Hint

Pop removes the last pushed element, so after pop(), the top is the previous element.

🧠 Conceptual
expert
1:00remaining
What is the time complexity of push operation in stack implemented using two queues?

In a stack implemented using two queues, the push operation involves moving elements between queues. What is its time complexity?

AO(n) linear time
BO(log n) logarithmic time
CO(1) constant time
DO(n^2) quadratic time
Attempts:
2 left
💡 Hint

Consider how many elements are moved during each push.