~/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.
Main path: 8 problems, about 3 hours.
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.
20 problems
Interview roadmap
Greedy Sort, then take the locally best choice.
- Play first: The River Crossing 1-minute game Greedy (farthest reach)
- Feed the most children basics py · c++ · java easy
- Fewest benches along the trail py · c++ · java easy
- Lemonade Change py · c++ · java easy
- Jump Game code the boss py · c++ · java medium
- Jump Game II py · c++ · java medium
- Non-overlapping Intervals ▶ boss game py · c++ · java medium
- Gas Station py · c++ · java medium
- Candy for a line of children Citadel py · c++ · java hard
More practice: 12 problems
- Order tasks to maximise deadline reward py · c++ · java easy
- Refactor obfuscated legacy code Citadel py · c++ · java easy
- Splitting a refund across payments Airbnb py · c++ · java easy
- Best Time to Buy and Sell Stock II py · c++ · java medium
- Merge Triplets to Form Target Triplet py · c++ · java medium
- Partition Labels py · c++ · java medium
- Maximum Task Rewards Airbnb py · c++ · java medium
- Hand of Straights py · c++ · java medium
- Valid Parenthesis String py · c++ · java medium
- Crack a 4-digit code with exact-match feedback Airbnb py · c++ · java medium
- Guess the Word Google py · c++ · java hard
- Link the clusters, spread the links Databricks py · c++ · java hard