GATE CSE 2023 Q59: Top of stack after mixed stack and queue operations
- Programming and Data Structures
- 2 marks
- NAT
- No negative marking
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 _______.
Try it: numerical answer: type the value
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
Source: official GATE 2023 CS question paper and final answer key, organised by IIT Kanpur. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q39Recursive length of a non-empty singly linked list
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15