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 fromS1and push it onS2.PushToS1: Pop the top element fromS2and push it onS1.GenerateOutput: Pop the top element fromS1and 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?
Official answer and solution
- Official answer
- B, C and D
- From
- Official GATE 2024 answer key
Solution
Check each candidate output sequence against the capacity limit of 2 on auxiliary stack S2.
- (A)
100, 200, 400, 300is IMPOSSIBLE: To output100first, all three elements above100(400, 300, 200) must be removed fromS1without being output, which would require holding elements inS2simultaneously, violatingS2's capacity of . - (B)
200, 300, 400, 100is POSSIBLE:PushToS2(400),PushToS2(300) , .GenerateOutputoutputs 200.PushToS1(300),GenerateOutputoutputs 300.PushToS1(400),GenerateOutputoutputs 400.GenerateOutputoutputs 100.
- (C)
400, 200, 100, 300is POSSIBLE:GenerateOutputoutputs 400.PushToS2(300) , .GenerateOutputoutputs 200.GenerateOutputoutputs 100.PushToS1(300),GenerateOutputoutputs 300.
- (D)
300, 200, 400, 100is POSSIBLE:PushToS2(400) , .GenerateOutputoutputs 300.GenerateOutputoutputs 200.PushToS1(400),GenerateOutputoutputs 400.GenerateOutputoutputs 100.
Options (B), (C), (D)
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2024 Computer Science and Information Technology - Set 2 (CS2)
- Question
- 48 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.