Question:medium

Consider a stack 𝑆 and a queue 𝑄. 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 𝑆 (pushed on 𝑆 ) or
to 𝑄 (enqueued to 𝑄). 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 𝑄 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 𝑆
or 𝑄.
Which of the following options is/are possible valid assignment(s) of the
elements?
Note: In the options, the notation π‘₯𝑆 denotes that element π‘₯ was assigned to 𝑆 and
𝑦𝑄 denotes that element 𝑦 was assigned to 𝑄.

Show Hint

Remember that emptying the stack always outputs its elements in reverse order of insertion (LIFO), and this segment always comes first in the output, followed by the queue's elements in their original insertion order (FIFO). Split the given output \(4\ 3\ 1\ 2\ 5\) into a stack-prefix and a queue-suffix and check each option against this rule.
Updated On: Aug 3, 2026
  • 1𝑆, 2𝑄, 3𝑆, 4𝑆, 5𝑄
  • 1𝑄, 2𝑄, 3𝑆, 4𝑆, 5𝑄
  • 1𝑄, 2𝑄, 3𝑄, 4𝑆, 5𝑆
  • 1𝑆, 2𝑆, 3𝑆, 4𝑄, 5𝑄
Show Solution

The Correct Option is A, B

Solution and Explanation

Instead of testing every option from scratch, first pin down which element must sit at the very bottom of the output. Since \(Q\) always empties after \(S\), and \(Q\) is FIFO, the *last* element in the given output, which is \(5\), is almost certainly the last element enqueued into \(Q\) (unless \(Q\) is empty, which cannot be since the output has 5 numbers and we only have limited stack depth constraints here). Also, whichever element sits at the very front of the output, here \(4\), must be the *last* element pushed onto \(S\) before emptying began, because a stack reverses arrival order on pop.

Reasoning from the front of the output: The output begins \(4, 3, 1\). For this to come purely from stack pops, element \(4\) was the most recently pushed among \(S\)-elements, \(3\) was pushed before \(4\), and \(1\) was pushed before \(3\). So if \(\{1,3,4\} \subseteq S\), their arrival order must have been \(1\) then \(3\) then \(4\) (which is consistent with the natural arrival order \(1,2,3,4,5\)), and the pop order \(4,3,1\) checks out automatically.

Reasoning from the back of the output: The remaining part \(2, 5\) must come from \(Q\) in the same order they were enqueued, meaning \(2\) was enqueued before \(5\). This is again consistent with arrival order since \(2\) arrives before \(5\).

Testing membership variants: The only requirement is that whichever elements go into \(S\) among \(\{1,2,3,4\}\) must, when popped, reduce to the prefix \(4,3,1\), and whichever go into \(Q\) must in enqueue order equal the remaining suffix \(2,5\). Checking each option: Option A (\(S = \{1,3,4\}\), \(Q=\{2,5\}\)) reduces to pop order \(4,3,1\) and queue order \(2,5\): matches. Option B (\(S=\{3,4\}\), \(Q=\{1,2,5\}\)) reduces to pop order \(4,3\) and queue order \(1,2,5\), giving combined \(4,3,1,2,5\): matches. Option C (\(S=\{4,5\}\), \(Q=\{1,2,3\}\)) gives pop order \(5,4\) and queue order \(1,2,3\), combined \(5,4,1,2,3\): does not match. Option D (\(S=\{1,2,3\}\), \(Q=\{4,5\}\)) gives pop order \(3,2,1\) and queue order \(4,5\), combined \(3,2,1,4,5\): does not match.

So the assignments that reproduce the output \(4\ 3\ 1\ 2\ 5\) are exactly those in Option A and Option B.

Final Answer: Options A and B are the possible valid assignments.

Was this answer helpful?
0


Questions Asked in GATE CS exam