IAE Academy

GATE 2022 CS Q.62: solution and answer

GATE 2022 · CSQ.62 · 2 marks · NAT

Consider the queues containing four elements and 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 required to place the elements of in in reverse order (shown as the Final State in the figure) without using any additional storage is ______.

GATE 2022 CS question 62: Two labeled box diagrams side by side.Tap to open full size
Figure from the GATE 2022 CS paper. It is part of the question.
Figure description

Two labeled box diagrams side by side. Left diagram, titled 'Initial State': an arrow labeled 'Head' points into the front of queue Q1, which is drawn as four cells left to right containing 1, 2, 3, 4 (so 1 is at the head/front); below it, queue Q2 is drawn as four empty cells, with an arrow labeled 'Head' pointing into its front (leftmost) cell from below. Right diagram, titled 'Final State': same layout — Q1 is drawn as four empty cells with a 'Head' arrow into its front; Q2 is drawn as four cells containing 4, 3, 2, 1 left to right (so 4 is at the head/front), with a 'Head' arrow into its front cell from below.

Answer and solution

ANSWER

0

Official answer: 0Source: Official answer key, GATE 2022 CS (IIT Kharagpur)

  1. The question counts only Enqueue operations performed on . Enqueue operations on and all Dequeue operations are free as far as the count is concerned. So we look for a method that never enqueues anything back into .

  2. Required final state: read from head to tail is . gives up its elements only in the order (head first), so each element has to be moved into place inside as it arrives. To do this we rotate : Dequeue its head and at once Enqueue that element back onto . The element is held for only one step, just as in any transfer between the two queues, so no additional storage is used.

  3. Move 1: Dequeue() gives 1, then Enqueue(, 1). Now . No rotation is needed.

  4. Move 2: Dequeue() gives 2, then Enqueue(, 2), so . Rotate once (take 1 off the head and put it at the tail): .

  5. Move 3: Dequeue() gives 3, then Enqueue(, 3), so . Rotate twice (move 2, then 1, to the tail): .

  6. Move 4: Dequeue() gives 4, then Enqueue(, 4), so . Rotate three times (move 3, 2, 1 to the tail): , which is the Final State. is empty.

  7. Count: every Enqueue above was on : transfers plus rotations, in total. The number of Enqueue operations on is .

  8. A count cannot be negative, so is the minimum. The minimum number of Enqueue operations on is .

KEY IDEA

Only Enqueue operations on are counted. Move each element of straight into , then rotate (Dequeue from , Enqueue back onto ) until the new element reaches the head. All of the reordering happens inside , so is never enqueued into and the minimum is 0.

WATCH OUT FOR

Doing the reordering by rotating to bring 4, then 3, then 2 to its head. That takes Enqueues on and gives 6. The rotations can be done on instead, where they do not count. A second mistake is counting every Enqueue on both queues (10) when the question asks only about .

From the GATE 2022 CS paper. Every question of this paper · Download the full paper · All 13 Linear structures questions, with notes

More Linear structures questions