GATE / CS ARCHIVE

GATE CSE 2021 SET 1 / QUESTION 21

GATE CSE 2021 Set 1 Q21

Data Structures · Stacks & Queues · Full GATE CSE 2021 Set 1 paper

GATE 2021 Set 1, Q21 1 mark · NAT
Report issue

Stacks & Queues

Question 21, GATE 2021: An initially empty stack executes push(54); push(52); pop(); push(55); push(62); s=pop(). An initially empty queue executes enqueue(21); enqueue(24); dequeue(); enqueue(28); enqueue(32); q=dequeue(). Find s+q.
Tap to enlarge
Answer & solution

Official answer: 86

Step through it (14 steps)

The first pop removes 52. After pushing 55 and 62, the next pop gives s=62. In the queue the first dequeue removes 21, leaving 24 at the front even after 28 and 32 are enqueued. Thus q=24 and s+q=86.

Write stack contents bottom-to-top: [54], [54,52], [54], [54,55], [54,55,62], then pop returns 62. Write queue contents front-to-back: [21], [21,24], [24], [24,28], [24,28,32], then dequeue returns 24. The stack is last-in-first-out and the queue first-in-first-out. Recording contents after every operation prevents confusing the earlier discarded pop/dequeue results with s and q.

Original paper, page 21 · Answer key