GATE / CS ARCHIVE

GATE CSE 2024 SET 2 / QUESTION 48

Generate output through two bounded stacks

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

GATE 2024 Set 2, Q48 2 marks · MSQ
Report issue

Stacks & Queues

Question 48, GATE 2024: S1 has capacity 4 and initially contains bottom-to-top 100,200,300,400. S2 has capacity 2 and is empty. T pops S1 into S2; R pops S2 into S1; O pops S1 to output. Empty pops and full pushes are forbidden. Which output orders are possible? A 100,200,400,300; B 200,300,400,100; C 400,200,100,300; D 300,200,400,100.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, C, D

Step through it (4 options)

Approach: Give legal operation witnesses for possible orders and a storage lower bound for the impossible order.

1. Derivation. Write T for PushToS2, R for PushToS1, and O for GenerateOutput. Starting with S1 bottom-to-top100, 200, 300, 400 and empty S2: B is realized by T, T, O, R, O, R, O, O; C by O, T, O, O, R, O; D by T, O, O, R, O, O. These produce the stated sequences without exceeding S2 capacity 2. A cannot start with 100: reaching it before any output requires all three higher elements outside S1, but S2 holds only two. Thus B, C, D.

2. Explanation and checks. For B, the first two T operations park 400 then 300 in S2, exposing 200. Output 200, restore and output 300, restore and output 400, then output 100. For C, output 400 first and park only 300; this exposes both 200 and 100 before restoring 300. For D, park only 400 and output 300,200 before restoring it. No hidden extra buffer is available, which is why starting with 100 is impossible.

Original paper, page 33 · Answer key