Skip to main contentSkip to solution

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.

OperationStack (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.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question