GATE / CS ARCHIVE

GATE CSE 2026 SET 2 / QUESTION 50

Reconstructing stack and queue assignments

Data Structures · Stacks & Queues · Full GATE CSE 2026 Set 2 paper

GATE 2026 Set 2, Q50 2 marks · MSQ
Report issue

Stacks & Queues

Question 50, GATE 2026: Items 1,2,3,4,5 arrive in order and are each placed in an initially empty stack S or queue Q (capacity 10 each). After all arrivals, empty S then Q to output 4,3,1,2,5. Possible assignments are A S={1,3,4}; B S={3,4}; C S={4,5}; D S={1,2,3}, with every other item in Q. Select all valid assignments.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A, B

Step through it (4 options)

Givens and goal

Items 1,2,3,4,5 arrive in order and are each placed in an initially empty stack S or queue Q (capacity 10 each). After all arrivals, empty S then Q to output 4,3,1,2,5. Possible assignments are A S={1,3,4}; B S={3,4}; C S={4,5}; D S={1,2,3}, with every other item in Q. Select all valid assignments.

Approach

The stack part reverses its arrival subsequence; the queue part preserves its arrival subsequence.

Step-by-step solution

1. For A, the stack receives 1,3,4 and pops 4,3,1. The queue receives 2,5 and dequeues 2,5. Concatenating the two outputs gives 4,3,1,2,5, so A works.
2. For B, the stack receives 3,4 and pops 4,3. The queue receives 1,2,5 and outputs 1,2,5. The same target output results, so B also works.
3. For C, the stack pops 5,4 and the queue outputs 1,2,3, producing 5,4,1,2,3. For D, the stack pops 3,2,1 and the queue outputs 4,5, producing 3,2,1,4,5. Both differ from the target.
4. Thus select A,B. The stack prefix must be decreasing and the queue suffix increasing, but the split point is not uniquely determined: here it can be after 4,3 or after 4,3,1. No interleaved dequeue or pop is permitted before all arrivals.

Original paper, page 36 · Answer key