Skip to content
Official PYQ · 2026

GATE CS 2026 Set 2 Q40 — Arrays, Stacks and Queues

Explain with AI

Consider a stack SS and a queue QQ. Both of them are initially empty and have the capacity to store ten elements each. The elements 1,2,3,41,2,3,4, and 55 arrive one by one, in that order. When an element arrives, it is assigned either to SS (pushed on SS ) or to QQ (enqueued to QQ ). 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 QQ is emptied by dequeueing all elements. The output obtained by following this process is 4312543125.


Given the output, the objective is to predict whether an element was assigned to SS or QQ.

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

Note: In the options, the notation xSx S denotes that element xx was assigned to SS and yQy Q denotes that element yy was assigned to QQ.

Select all correct options
View worked solution

Answer: A;B

To find the valid assignments, let us understand how the output is generated:

  1. Stack SS (LIFO): If elements are pushed into SS in the order of their arrival, popping them all will output them in the reverse order of their arrival.
  2. Queue QQ (FIFO): If elements are enqueued into QQ in the order of their arrival, dequeueing them all will output them in the same order as their arrival.

The final output is the concatenation of the popped elements from SS followed by the dequeued elements from QQ:

Output=(Reverse of elements in S)⋅(Elements in Q in arrival order)\text{Output} = (\text{Reverse of elements in } S) \cdot (\text{Elements in } Q \text{ in arrival order})

We need the final output to be 4312543125. Let us test each option:


Option A: 1S,2Q,3S,4S,5Q1 S, 2 Q, 3 S, 4 S, 5 Q

  • Elements assigned to SS: 1,3,41, 3, 4 (in arrival order)
  • Elements assigned to QQ: 2,52, 5 (in arrival order)
  • Output from SS (reversed): 4,3,14, 3, 1
  • Output from QQ (same order): 2,52, 5
  • Combined Output: 4312543125 (Matches)

Option B: 1Q,2Q,3S,4S,5Q1 Q, 2 Q, 3 S, 4 S, 5 Q

  • Elements assigned to SS: 3,43, 4 (in arrival order)
  • Elements assigned to QQ: 1,2,51, 2, 5 (in arrival order)
  • Output from SS (reversed): 4,34, 3
  • Output from QQ (same order): 1,2,51, 2, 5
  • Combined Output: 4312543125 (Matches)

Option C: 1Q,2Q,3Q,4S,5S1 Q, 2 Q, 3 Q, 4 S, 5 S

  • Elements assigned to SS: 4,54, 5 (in arrival order)
  • Elements assigned to QQ: 1,2,31, 2, 3 (in arrival order)
  • Output from SS (reversed): 5,45, 4
  • Output from QQ (same order): 1,2,31, 2, 3
  • Combined Output: 5412354123 (Does not match)

Option D: 1S,2S,3S,4Q,5Q1 S, 2 S, 3 S, 4 Q, 5 Q

  • Elements assigned to SS: 1,2,31, 2, 3 (in arrival order)
  • Elements assigned to QQ: 4,54, 5 (in arrival order)
  • Output from SS (reversed): 3,2,13, 2, 1
  • Output from QQ (same order): 4,54, 5
  • Combined Output: 3214532145 (Does not match)

Thus, the valid assignments are A and B.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Arrays, Stacks and Queues questions