GATE Computer ScienceModerateGATE CS (PYQ pattern)

A queue holds 5, 12, 7, 3, 9, 14, 2, 8 (front first). To reverse the first 5 elements, they are dequeued onto a stack, popped back and enqueued at the rear, and then the remaining elements are each moved (dequeue + enqueue) from the front to the rear. How many enqueue operations are made in total, and what is the final queue from the front?

  1. A. 5 enqueues; 9 3 7 12 5 14 2 8
  2. B. 8 enqueues; 9 3 7 12 5 14 2 8
  3. C. 8 enqueues; 5 12 7 3 9 14 2 8
  4. D. 8 enqueues; 9 3 7 12 5 8 2 14

Correct answer: B. 8 enqueues; 9 3 7 12 5 14 2 8

The 5 popped items go to the rear in reverse order (9, 3, 7, 12, 5) behind 14, 2, 8; rotating those three behind them restores the tail order, so the final queue is 9 3 7 12 5 14 2 8. Total enqueues 5 + 3 = 8.

More Programming and Data Structures (GATE) questions

Exams that ask this topic