GATE CS 2024 Set 2 — Question 48
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.MSQ+2 / -0HardStack OperationsStacks & QueuesProgramming & Data Structures
Programming & Data Structures → Stacks & Queues → Stack Operations
Last updated
Question
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.
Correct answer
(B) 200, 300, 400, 100; (C) 400, 200, 100, 300; (D) 300, 200, 400, 100
Solution
The initial state of Stack S1 is where is at the top. Stack S2 is empty with capacity . S1 has capacity .Let's analyze each option:(A) 100, 200, 400, 300
To output first, we must pop from S1 and store them in S2. This requires S2 to hold elements. Since S2 capacity is , this is impossible.(B) 200, 300, 400, 100
To output first, we must pop from S1 and store them in S2. This requires S2 to hold elements. Since S2 capacity is , this is impossible.(B) 200, 300, 400, 100
1.
PushToS2 (400). S1: , S2: .2.
PushToS2 (300). S1: , S2: . (S2 is full).3.
GenerateOutput (200). Output: 200. S1: .4.
PushToS1 (300). S1: , S2: .5.
GenerateOutput (300). Output: 300. S1: .6.
PushToS1 (400). S1: , S2: .7.
GenerateOutput (400). Output: 400. S1: .8.
This sequence is possible.(C) 400, 200, 100, 300GenerateOutput (100). Output: 100. S1: .1.
GenerateOutput (400). Output: 400. S1: .2.
PushToS2 (300). S1: , S2: .3.
GenerateOutput (200). Output: 200. S1: .4.
GenerateOutput (100). Output: 100. S1: .5.
PushToS1 (300). S1: , S2: .6.
This sequence is possible.(D) 300, 200, 400, 100GenerateOutput (300). Output: 300. S1: .1.
PushToS2 (400). S1: , S2: .2.
GenerateOutput (300). Output: 300. S1: .3.
GenerateOutput (200). Output: 200. S1: .4.
PushToS1 (400). S1: , S2: .5.
GenerateOutput (400). Output: 400. S1: .6.
This sequence is possible.Thus, options (B), (C), and (D) can be generated.GenerateOutput (100). Output: 100. S1: .Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on Stacks & Queues
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]