Question
Consider the following sequence of operations on an empty stack.
push(54); push(52); pop(); push(55); push(62); s = pop();
Consider the following sequence of operations on an empty queue.
enqueue(21); enqueue(24); dequeue(); enqueue(28); enqueue(32); q = dequeue();
The value of is __________
Official answer and solution
- Official answer
- 86
- From
- Official GATE 2021 answer key
Solution
Trace the step-by-step state of the stack and the queue to find the values of s and q.
Trace the stack operations (initially empty):
push(54): stack is[54]push(52): stack is[54, 52]pop(): removes 52; stack is[54]push(55): stack is[54, 55]push(62): stack is[54, 55, 62]s = pop(): removes and returns 62. Thus .
Trace the queue operations (initially empty, FIFO order):
enqueue(21): queue is[21]enqueue(24): queue is[21, 24]dequeue(): removes 21; queue is[24]enqueue(28): queue is[24, 28]enqueue(32): queue is[24, 28, 32]q = dequeue(): removes and returns the front element, 24. Thus .
Computing the sum:
86
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2021 Computer Science and Information Technology (CS, Set-1)
- Question
- 31 of 65, printed as CS Q.21
- 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.