~/problems

Problems

Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.

Stacks

Stacks

Matching pairs, undo history and evaluating expressions with a stack.

Notes

Recognise it when: the most recent unfinished thing has to be dealt with first: an opening bracket waiting for its match, an operand waiting for its operator, a state to return to.

  • Python: a list is the stack. append pushes, pop() pops, stack[-1] peeks; check if stack before peeking.
  • Pair each push with enough context (an index, a running minimum, a count) to answer later questions in O(1).
  • A second stack often fixes the one thing a single stack can't: the minimum so far, or the reverse order for a queue.
  • Expression problems: numbers go on one stack; when an operator or a closing bracket arrives, pop and combine.

Common mistakes: popping from an empty stack; forgetting leftovers when the input ends (unmatched openers, a number still being built).

8 problems

Interview roadmap

Stacks Matching brackets, paths, next greater element.

Stacks guide

esc