The GATE Grind

GATE 2024 CS (CS2) – Question 48

Programming and Data Structures · Stacks and Queues · 2 marks · Multiple select

Let S1 and S2 be two stacks. S1 has capacity 4 and already holds 100, 200, 300, 400 (400 on top). S2 has capacity 2 and is empty. Operations: PushToS2 (pop from S1, push on S2), PushToS1 (pop from S2, push on S1), GenerateOutput (pop from S1 and output). Pop on an empty stack and push on a full stack are not allowed. Which of the following output sequences can be generated?

  1. 100, 200, 400, 300
  2. 200, 300, 400, 100
  3. 400, 200, 100, 300
  4. 300, 200, 400, 100

Practise this question in The GATE Grind →

Show answer and explanation

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

Explanation

S1 holds 100, 200, 300, 400 (400 on top, capacity 4). S2 is empty with capacity 2. Operations: move S1→S2, move S2→S1, output from top of S1.

Answer: B, C, D.

More Stacks and Queues questions

All Stacks and Queues questions →