The GATE Grind

GATE 2022 CS – Question 62

Programming and Data Structures · Stacks and Queues · 2 marks · Numerical answer

Consider the queues $Q_1$ containing four elements (1, 2, 3, 4 with 1 at the head) and $Q_2$ containing none (shown as the Initial State in the figure). The only operations allowed on these two queues are Enqueue(Q,element) and Dequeue(Q). The minimum number of Enqueue operations on $Q_1$ required to place the elements of $Q_1$ in $Q_2$ in reverse order (final state of $Q_2$ from head: 4, 3, 2, 1) without using any additional storage is ___________.

Diagram for GATE 2022 CS question 62

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 6

Explanation

To move 4 to Q2 first, rotate Q1 three times (3 enqueues on Q1). Then 3 needs two rotations, and 2 needs one. The total is 3+2+1 = 6 enqueues on Q1.