~/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.
Greedy with sorting
Sort, then take the locally best choice; prove it with an exchange argument.
Notes
Recognise it when: a locally best choice never hurts. Typical shapes: interval scheduling, the earliest deadline, the smallest or largest first, "can we reach the end".
- Max non-overlapping intervals: sort by end and take every interval that starts after the last one you took ended.
- Jump game: track the farthest index reachable so far. If
i > farthest, you're stuck. - Gas station: if total gas < total cost, it's impossible. Otherwise, restart from
i + 1whenever the running tank goes negative.
Prove it (or at least argue it) with an exchange argument: swapping any optimal solution's first choice for the greedy choice never makes it worse.
Gotchas: many problems look greedy but need DP. Coin change with arbitrary denominations is the classic trap, so test a counterexample before committing.
21 problems
Interview roadmap
Greedy Sort, then take the locally best choice.
- Basics: feed the most children basics py · c++ · java easy
- Fewest benches along the trail py · c++ · java easy
- Non-overlapping Intervals py · c++ · java medium
- Jump Game py · c++ · java easy
- Gas Station py · c++ · java medium
- Fewest gondolas for pairs of children py · c++ · java easy
- Order tasks to maximise deadline reward py · c++ · java easy
- Candy for a line of children Citadel py · c++ · java medium
- Refactor obfuscated legacy code Citadel easy
- Jump Game II py · c++ · java medium
- Crack a 4-digit code with exact-match feedback Airbnb medium
- Maximum Task Rewards Airbnb medium
- Splitting a refund across payments Airbnb easy
- Link the clusters, spread the links Databricks py · c++ · java hard
- Where do 1..k sit together? Uber py · c++ · java medium
- Hand of Straights py · c++ · java medium
- Merge Triplets to Form Target Triplet py · c++ · java medium
- Partition Labels py · c++ · java medium
- Valid Parenthesis String py · c++ · java medium
- Best Time to Buy and Sell Stock II py · c++ · java easy
- Lemonade Change py · c++ · java easy