GATE CS 2024 Set 2 — Question 48
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.
A.
100, 200, 400, 300
B.
200, 300, 400, 100
C.
400, 200, 100, 300
D.
300, 200, 400, 100
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: .More questions on Stacks & Queues
2024 Set 2 Q13Consider the following C program. Assume parameters to a function are evaluated from right to left.…2024 Set 2 Q17Let be the adjacency matrix of a simple undirected graph . Suppose is its own inverse.…2024 Set 1 Q17Given an integer array of size , we want to check if the array is sorted (in either ascending or…2024 Set 1 Q18Consider the following C program: [code] Which one of the following statements is CORRECT?2024 Set 1 Q19Consider the following C program: [code] Assume that the input to the program from the command line…
Practice GATE CS PYQs with adaptive difficulty
Timed practice, skill tracking, and AI explanations — free to start.
Start practicing free