GateAI
Stacks

GATE CSE 2024 Set 2 · Q48

MSQ2 marksMedium

Time

Let S1S1 and S2S2 be two stacks. S1S1 has capacity of 4 elements. S2S2 has capacity of 2 elements. S1S1 already has 4 elements: 100, 200, 300, and 400, whereas S2S2 is empty, as shown below. Only the following three operations are available: PushToS2: Pop the top element from S1S1 and push it on S2S2. PushToS1: Pop the top element from S2S2 and push it on S1S1. GenerateOutput: Pop the top element from S1S1 and output it to the user. Note that the pop operation is not allowed on an empty stack and the push operation is not allowed on a full stack. Which of the following output sequences can be generated by using the above operations?

GATE CSE 2024 Set 2 Q48 diagram

Select all that applyOne or more correct

Pick every correct option

Solution

Step-by-step verified solution

Official answer: B) 200, 300, 400, 100; C) 400, 200, 100, 300; D) 300, 200, 400, 100

Stacks

Initial state: S1=[100,200,300,400]S_1 = [100, 200, 300, 400] (top is 400400, capacity 4), S2=[]S_2 = [] (capacity 2).

  • Option A (100,200,400,300100, 200, 400, 300): To output 100100 first, all 3 elements above it (400,300,200400, 300, 200) must be removed from S1S_1. Since S2S_2 has a capacity of only 22, it cannot hold all three elements simultaneously. Thus, this sequence is impossible.

  • Option B (200,300,400,100200, 300, 400, 100):

    1. PushToS2 (moves 400400 to S2S_2)

    2. PushToS2 (moves 300300 to S2S_2)

    3. GenerateOutput→200\rightarrow 200

    4. PushToS1 (moves 300300 to S1S_1)

    5. GenerateOutput→300\rightarrow 300

    6. PushToS1 (moves 400400 to S1S_1)

    7. GenerateOutput→400\rightarrow 400

    8. GenerateOutput→100\rightarrow 100

    Valid.

  • Option C (400,200,100,300400, 200, 100, 300):

    1. GenerateOutput→400\rightarrow 400

    2. PushToS2 (moves 300300 to S2S_2)

    3. GenerateOutput→200\rightarrow 200

    4. GenerateOutput→100\rightarrow 100

    5. PushToS1 (moves 300300 to S1S_1)

    6. GenerateOutput→300\rightarrow 300

    Valid.

  • Option D (300,200,400,100300, 200, 400, 100):

    1. PushToS2 (moves 400400 to S2S_2)

    2. GenerateOutput→300\rightarrow 300

    3. GenerateOutput→200\rightarrow 200

    4. PushToS1 (moves 400400 to S1S_1)

    5. GenerateOutput→400\rightarrow 400

    6. GenerateOutput→100\rightarrow 100

    Valid.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Stacks previous year question is Q48 of the GATE CSE 2024 Set 2 paper. Find more GATE CSE PYQs on Stacks in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 3 related Stacks PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Stacks in GATE CSE

Stacks has 3 GATE CSE PYQs across 3 papers from 2015 to 2025. 2 of them are 2-mark MSQs like this one, and it is the only one in GATE CSE 2024 Set 2.

How GATE tests Stacks

More from GATE CSE 2024 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2024 Set 2 · Q48 · MSQ

Let S1 and S2 be two stacks. S1 has capacity of 4 elements. S2 has capacity of 2 elements. S1 already has 4 elements: 100, 200, 300, and 400, whereas S2 is empty, as shown below. Only the following three operations are available: PushToS2 : Pop the top element from S1 and push it on S2. PushToS1 : Pop the top element from S2 and push it on S1. GenerateOutput : Pop the top element from S1 and output it to the user. Note that the pop operation is not allowed on an empty stack and the push operation is not allowed on a full stack. Which of the following output sequences can be generated by using the above operations?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.