Skip to main content

GATE CSE 2026Data Structures › Stacks, Queues, Linked Lists & ArraysVerified

GATE CSE 2026 PYQ: Consider a stack S S S and a queue Q Q Q . Both of them are initially empty and have the capacity to store…

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,4, and 5 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 43125.


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.

  1. A
    1S,2Q,3S,4S,5Q1 S, 2 Q, 3 S, 4 S, 5 Q
    Correct answer
  2. B
    1Q,2Q,3S,4S,5Q1 Q, 2 Q, 3 S, 4 S, 5 Q
    Correct answer
  3. C
    1Q,2Q,3Q,4S,5S1 Q, 2 Q, 3 Q, 4 S, 5 S
  4. D
    1S,2S,3S,4Q,5Q1 S, 2 S, 3 S, 4 Q, 5 Q

Answer

(A, B) 1 S , 2 Q , 3 S , 4 S , 5 Q 1 S, 2 Q, 3 S, 4 S, 5 Q 1 S , 2 Q , 3 S , 4 S , 5 Q; 1 Q , 2 Q , 3 S , 4 S , 5 Q 1 Q, 2 Q, 3 S, 4 S, 5 Q 1 Q , 2 Q , 3 S , 4 S , 5 Q

Approach

The final output is the concatenation of two sequences: first, the stack elements popped in LIFO order (reverse of their arrival order), and second, the queue elements dequeued in FIFO order (their original arrival order). So for each proposed assignment, compute the stack pop sequence and the queue dequeue sequence and check whether they concatenate to 4,3,1,2,5.

Only assignments A and B produce the required sequence 4,3,1,2,5. In both cases the stack contributes the prefix 4,3,1 and the queue contributes the suffix 2,5. Options C and D produce different stack/queue orders and fail to match the target output.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.