Question:

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: Jul 7, 2026
  • 1𝑆, 2𝑄, 3𝑆, 4𝑆, 5𝑄
  • 1𝑄, 2𝑄, 3𝑆, 4𝑆, 5𝑄
  • 1𝑄, 2𝑄, 3𝑄, 4𝑆, 5𝑆
  • 1𝑆, 2𝑆, 3𝑆, 4𝑄, 5𝑄
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is A, B

Solution and Explanation

We have a stack \(S\) and a queue \(Q\), both empty initially. Elements \(1,2,3,4,5\) arrive in order and each is either pushed onto \(S\) or enqueued into \(Q\). Once all five are placed, we first pop all of \(S\) (LIFO order, so the last element pushed comes out first), then we dequeue all of \(Q\) (FIFO order, so elements come out in the same order they were enqueued). The final output must be \(4\ 3\ 1\ 2\ 5\).

Step 1: Understand the output structure. The output is always [stack-pop sequence] followed by [queue-dequeue sequence]. So we must split \(4\ 3\ 1\ 2\ 5\) into a prefix that is a valid LIFO pop order of the S-elements and a suffix that is a valid FIFO order of the Q-elements, consistent with a given assignment.

Step 2: Check Option A - \(1S, 2Q, 3S, 4S, 5Q\). Elements pushed on \(S\) in arrival order are \(1, 3, 4\). Popping (LIFO) reverses this order: \(4, 3, 1\). Elements sent to \(Q\) in arrival order are \(2, 5\); dequeuing keeps this order: \(2, 5\). Combined output = \(4\ 3\ 1\ 2\ 5\), which exactly matches the required output. So Option A is valid.

Step 3: Check Option B - \(1Q, 2Q, 3S, 4S, 5Q\). Elements on \(S\) in arrival order: \(3, 4\). Popping gives \(4, 3\). Elements on \(Q\) in arrival order: \(1, 2, 5\). Dequeuing gives \(1, 2, 5\). Combined output = \(4\ 3\ 1\ 2\ 5\), which matches. So Option B is valid.

Step 4: Check Option C - \(1Q, 2Q, 3Q, 4S, 5S\). Elements on \(S\): \(4, 5\). Popping gives \(5, 4\). Elements on \(Q\): \(1, 2, 3\). Dequeuing gives \(1, 2, 3\). Combined output = \(5\ 4\ 1\ 2\ 3\), which does not match \(4\ 3\ 1\ 2\ 5\). So Option C is invalid.

Step 5: Check Option D - \(1S, 2S, 3S, 4Q, 5Q\). Elements on \(S\): \(1, 2, 3\). Popping gives \(3, 2, 1\). Elements on \(Q\): \(4, 5\). Dequeuing gives \(4, 5\). Combined output = \(3\ 2\ 1\ 4\ 5\), which does not match. So Option D is invalid.

Step 6: Conclusion. Only the assignments in Option A and Option B reproduce the exact output sequence \(4\ 3\ 1\ 2\ 5\) when the stack is emptied first (LIFO) and then the queue is emptied (FIFO).

Final Answer: \[\boxed{\text{Options A and B are correct}}\]

Was this answer helpful?
0
0

Top GATE CS Computer Science and IT Engineering Questions

View More Questions