Question:medium

Consider a stack \(S\) and a queue \(Q\). Both of them are initially empty and have the capacity to store ten elements each. The elements \(1, 2, 3, 4\), and \(5\) arrive one by one, in that order. When an element arrives, it is assigned either to \(S\) (pushed on \(S\)) or to \(Q\) (enqueued to \(Q\)). Once all the five elements are stored, the output is generated in two steps. First, stack \(S\) is emptied by popping all elements. Then queue \(Q\) is emptied by dequeueing all elements. The output obtained by following this process is \(4\ 3\ 1\ 2\ 5\).

Given the output, the objective is to predict whether an element was assigned to \(S\) or \(Q\).

Which of the following options is/are possible valid assignment(s) of the elements?

Note: In the options, the notation \(xS\) denotes that element \(x\) was assigned to \(S\) and \(yQ\) denotes that element \(y\) was assigned to \(Q\).

Show Hint

Reverse only the elements sent to the stack, keep the elements sent to the queue in their original arrival order, and check which split matches 4 3 1 2 5.
Updated On: Jul 22, 2026
  • \(1S, 2Q, 3S, 4S, 5Q\)
  • \(1Q, 2Q, 3S, 4S, 5Q\)
  • \(1Q, 2Q, 3Q, 4S, 5S\)
  • \(1S, 2S, 3S, 4Q, 5Q\)
Show Solution

The Correct Option is A, B

Solution and Explanation

Step 1: Split the target output at every possible point.
The full output is $4\,3\,1\,2\,5$, five numbers. It is built as the reversed stack sequence followed by the queue sequence in order, so the target string must break into a front part that is some reversal of the numbers pushed to $S$, and a back part that is exactly the numbers pushed to $Q$ in order. We try each option's stack elements, reverse them, and see if they line up with the front of the target.

Step 2: Check option A, stack elements $\{1,3,4\}$ pushed in order $1,3,4$.
Reversing $1,3,4$ gives $4,3,1$, which is exactly the first three numbers of the target $4,3,1,2,5$. The queue elements $\{2,5\}$ in order $2,5$ match the remaining tail $2,5$ exactly. Both halves line up, so A works.

Step 3: Check option B, stack elements $\{3,4\}$ pushed in order $3,4$.
Reversing gives $4,3$, matching the first two numbers of the target. The queue elements $\{1,2,5\}$ in order $1,2,5$ match the remaining tail $1,2,5$ exactly. So B also works.

Step 4: Check option C, stack elements $\{4,5\}$ pushed in order $4,5$.
Reversing gives $5,4$. The target output starts with $4,3$, not $5,4$, so this already fails to match the front of the target. C does not work.

Step 5: Check option D, stack elements $\{1,2,3\}$ pushed in order $1,2,3$.
Reversing gives $3,2,1$, which does not match the required front $4,3,1$ of the target either, since it starts with $3$ instead of $4$. D does not work.

Step 6: Conclude.
Only the assignments in A and B reverse to the correct front section and leave the correct tail section for the queue.
\[ \boxed{\text{Options A and B are the valid assignments}} \]
Was this answer helpful?
0

Questions Asked in GATE CS exam