~/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
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.
appendpushes,pop()pops,stack[-1]peeks; checkif stackbefore 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
- Basics: an undo log with a stack basics py · c++ · java easy
- Valid Parentheses py · c++ · java easy
- Implement Queue Using Stacks py · c++ · java easy
- Min Stack py · c++ · java medium
- Evaluate Reverse Polish Notation py · c++ · java medium
- Asteroid Collision py · c++ · java medium
- Decode String py · c++ · java medium
- Maximum Frequency Stack py · c++ · java hard