LeetCode patterns
A 32-chapter guide to the techniques behind most interview problems, and to telling which one a problem needs. It links 125 practice problems. It doesn't cover every problem, and it doesn't claim to.
A pattern is more than a data structure. A heap is a tool; "only the best k candidates can affect the answer" is the reason to reach for one. Each chapter teaches that reason.
Where to start
New here? Read these three chapters first:
Then pick your next topic from the suggested order, which shows which chapters build on which.
Learn first, then test yourself
- When you're learning, use worked examples and hints freely. Then close them and rebuild the solution from a blank page.
- When you're testing yourself, pick a problem without looking at its topic, and solve it without help. Keep track of the two kinds of results separately.
- Come back to old problems after a few days, and also try a variation. Remembering one solution and choosing the right approach for a new problem are different skills.
The transfer exercises change familiar problems in ways that break the usual method. The hints and answers are on a separate page.
The chapters
- 01. How to use this book
- 02. Derive and justify a solution
- 03. Reasoning moves that unlock problems
- 04. A map of useful observations
- 05. Hashing and sufficient summaries
- 06. Prefix sums and algebraic transformations
- 07. Two pointers and safe elimination
- 08. Sliding windows and monotone validity
- 09. Binary search over a boundary
- 10. Stacks and monotonic deques
- 11. Heaps and the candidate frontier
- 12. Sorting intervals and greedy choices
- 13. Linked lists and recursive trees
- 14. Backtracking and the search tree
- 15. Graph modeling and traversal
- 16. Dependencies and merging components
- 17. Weighted shortest paths
- 18. Dynamic programming from sufficient state
- 19. Dynamic programming families
- 20. Tries and string structure
- 21. Bits mathematics and small state spaces
- 22. Range queries under updates
- 23. Data structure design and composed invariants
- 24. Rust as an implementation tool
- 25. Similar looking problems with different rules
- 26. A curriculum built around dependencies
- 27. Transfer exercises
- 28. Hints and transfer exercise answers
- 29. Review pages and contribution method
- 30. Advanced families and when they become useful
- 31. Sources and further reading
- 32. Selected problem index
Combine several ideas
The LRU cache and file system labs each combine several techniques in one design. Try them once single patterns feel comfortable.
Prefer paper? The PDF edition is the original guide. The website has grown beyond it.