Question
Consider a sequence of elements , and . The following operations are performed on a stack and a queue , both of which are initially empty.
I: push the elements of from to in that order into .
II: enqueue the elements of from to in that order into .
III: pop an element from .
IV: dequeue an element from .
V: pop an element from .
VI: dequeue an element from .
VII: dequeue an element from and push the same element into .
VIII: Repeat operation VII three times.
IX: pop an element from .
X: pop an element from .
The top element of after executing the above operations is _______.
Official answer and solution
- Official answer
- 8
- From
- Official GATE 2023 answer key
Solution
Simulate step-by-step the stack and queue operations.
Given sequence :
- I: Push into : (top is 2).
- II: Enqueue into : (front is 1).
- III: Pop from : pops 2 .
- IV: Dequeue from : dequeues 1 .
- V: Pop from : pops 9 .
- VI: Dequeue from : dequeues 5 .
- VII: Dequeue from and push to : dequeues 7, pushes 7 .
- VIII: Repeat VII 3 times:
- Dequeue 8, push 8 .
- Dequeue 9, push 9 .
- Dequeue 2, push 2 .
- IX: Pop from : pops 2 .
- X: Pop from : pops 9 .
The top element of is now 8.
8
Losing track of the repeated dequeue-and-push operations in step VIII.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2023 Computer Science and Information Technology (CS)
- Question
- 59 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.