Consider a stack with a maximum capacity of 5. The following operations take place on that stack:
PUSH(5), PUSH(6), POP, PUSH(7), POP, PUSH(8)
What are the contents of the stack after these operations?
Consider a stack with a maximum capacity of 5. The following operations take place on that stack:
PUSH(5), PUSH(6), POP, PUSH(7), POP, PUSH(8)
What are the contents of the stack after these operations?
Solution
✅ Correct Option: 2
A stack is LIFO (last in, first out), so POP always removes the most recently pushed element.
| Operation | Stack (bottom to top) |
|---|---|
| PUSH(5) | 5 |
| PUSH(6) | 5, 6 |
| POP (removes 6) | 5 |
| PUSH(7) | 5, 7 |
| POP (removes 7) | 5 |
| PUSH(8) | 5, 8 |
The capacity of 5 is never reached, so there is no overflow.
Option 1: this is what a queue (FIFO) would give, where the oldest element is removed first.
Related questions:
2026: 18 May Shift 2
2026: 26 May Shift 1
2026: 31 May Shift 1
2025: 15 May Shift 2