Skip to content
GATEiit

GATE CSE 2023 Q59: Top of stack after mixed stack and queue operations

Question

Consider a sequence aa of elements a0=1a_0 = 1, a1=5a_1 = 5, a2=7a_2 = 7, a3=8a_3 = 8, a4=9a_4 = 9, and a5=2a_5 = 2. The following operations are performed on a stack SS and a queue QQ, both of which are initially empty.

I: push the elements of aa from a0a_0 to a5a_5 in that order into SS.

II: enqueue the elements of aa from a0a_0 to a5a_5 in that order into QQ.

III: pop an element from SS.

IV: dequeue an element from QQ.

V: pop an element from SS.

VI: dequeue an element from QQ.

VII: dequeue an element from QQ and push the same element into SS.

VIII: Repeat operation VII three times.

IX: pop an element from SS.

X: pop an element from SS.

The top element of SS after executing the above operations is _______.

Try it: numerical answer: type the value

Numerical answer. No negative marking.
Show answer and explanation

Answer: 8 (official GATE 2023 final answer key)

Concept used: Stack is last-in first-out; queue is first-in first-out.

First step: After III–VI the stack holds 1, 5, 7, 8 (top 8) and the queue holds 7, 8, 9, 2; continue from there.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2023 CS question paper and final answer key, organised by IIT Kanpur. Question paper · Answer key