Stack problems, grouped by pattern

A stack remembers the most recent thing you have not dealt with yet. That makes it the natural tool for matching brackets, undoing operations, simulating collisions and evaluating expressions.

When to reach for it

  • Something opens and must later be closed
  • The latest item decides what happens next
  • You need to evaluate or parse an expression

Mistakes to watch for

  • Popping from an empty stack
  • Leaving unmatched items on the stack at the end
  • Mixing operand order for subtraction and division

The 7 problems

1Stack Basics