GATE / CS ARCHIVE

GATE CSE 1991 / QUESTION 3(vii)

Tracing stack operations

Data Structures · Stacks & Queues · Full GATE CSE 1991 paper

GATE 1991, Q3(vii) 2 marks · MSQ
Report issue

Stacks & Queues

Question 3(vii), GATE 1991: Execute PUSH 10, PUSH 20, POP, PUSH 10, PUSH 20, POP, POP, POP, PUSH 20, POP. Options include A20, 10, 20, 10, 20; B20, 20, 10, 10, 20; C10, 20, 20, 10, 20; D20, 20, 10, 20, 10.
Question 3(vii), GATE 1991: Execute PUSH 10, PUSH 20, POP, PUSH 10, PUSH 20, POP, POP, POP, PUSH 20, POP. Options include A20, 10, 20, 10, 20; B20, 20, 10, 10, 20; C10, 20, 20, 10, 20; D20, 20, 10, 20, 10.
Tap to enlarge
Select all that apply
Answer & solution

Answer: B: 20, 20, 10, 10, 20 (no official key was published)

Step through it (12 steps)

After the first two pushes, the stack from bottom to top is [10, 20], so the first pop yields 20. Pushing 10 then 20 leaves [10, 10, 20]; the next three pops yield 20, 10, 10 and empty the stack. The final push/pop yields 20. Concatenating these five outputs gives 20, 20, 10, 10, 20. No pop is attempted on an empty stack. The result follows last-in-first-out order, not the chronological order of pushes as a queue would use.

Original paper, page 3