GATE Computer ScienceModerateGATE CSE

A queue is implemented with two stacks: ENQ pushes onto stack S1; DEQ pops from S2, and if S2 is empty it first moves every element of S1 into S2 (popping S1, pushing S2). The operations ENQ 1, ENQ 2, ENQ 3, DEQ, ENQ 4, DEQ, ENQ 5, DEQ, DEQ are executed. What are the four values returned by DEQ, in order?

  1. A. 1, 2, 4, 3
  2. B. 1, 2, 3, 4
  3. C. 3, 2, 1, 4
  4. D. 1, 3, 2, 4

Correct answer: B. 1, 2, 3, 4

Queue order is preserved: elements leave in the order 1, 2, 3, 4 (the reversal into S2 restores FIFO order).

More Programming and Data Structures (GATE) questions

Exams that ask this topic