There are thousands of LeetCode problems and you will never finish them. You don't need to. Interview problems are variations on roughly fifteen recurring patterns - learn to recognise each one and you can solve problems you've never seen, which is the entire point.
Why patterns beat grinding
Grinding random problems trains recall - you get good at problems you've seen. Patterns train recognition - you get good at mapping a new problem to a technique you already know. Interviews are always new problems, so recognition is the skill that transfers.
The 15 core patterns
For each pattern: the signals that it applies, and a few canonical problems to drill it. If you can solve two or three problems per row here, you cover most of what interviews throw at you.
| Pattern | Recognise it when⦠| Practice on |
|---|---|---|
| Two pointers | Sorted array/string, pair or triplet with a target, in-place partition | Two Sum II, 3Sum, Container With Most Water |
| Sliding window | Longest/shortest contiguous subarray or substring meeting a condition | Longest Substring Without Repeating, Min Window Substring |
| Fast & slow pointers | Cycle detection, find middle, happy-number style loops | Linked List Cycle, Find the Duplicate Number |
| Merge intervals | Overlapping intervals, scheduling, merging ranges | Merge Intervals, Meeting Rooms II |
| Binary search | Sorted data, or "search on the answer" for a monotonic function | Search in Rotated Array, Koko Eating Bananas |
| BFS | Shortest path / level-order on unweighted graphs and trees | Binary Tree Level Order, Rotting Oranges |
| DFS / backtracking | Explore all paths, permutations, combinations, subsets | Subsets, Word Search, N-Queens |
| Top-K / heap | "K largest/smallest/most frequent", running medians | Top K Frequent Elements, Kth Largest |
| Hash map / set | O(1) lookup, counting, dedup, grouping | Two Sum, Group Anagrams, Longest Consecutive |
| Dynamic programming | Optimal substructure + overlapping subproblems | Coin Change, House Robber, Edit Distance |
| Greedy | Locally optimal choice yields global optimum | Jump Game, Gas Station |
| Topological sort | Ordering with dependencies / DAGs | Course Schedule, Alien Dictionary |
| Union-find | Connected components, cycle detection in undirected graphs | Number of Provinces, Redundant Connection |
| Trie | Prefix search, autocomplete, word dictionaries | Implement Trie, Word Search II |
| Monotonic stack | Next greater/smaller element, spans, histograms | Daily Temperatures, Largest Rectangle in Histogram |
How to recognize a pattern
Patterns announce themselves through keywords and input shape. Train these triggers until they're automatic:
- "Sorted" + pair/triplet/target β two pointers or binary search.
- "Contiguous" / "substring" / "window" β sliding window.
- "All combinations / permutations / subsets" β backtracking.
- "Shortest path" on unweighted graph β BFS.
- "Top / Kth / most frequent" β heap.
- "Number of ways" / "min/max cost" with choices β dynamic programming.
- "Order with prerequisites" β topological sort.
- "Next greater/smaller" β monotonic stack.
Complexity cheat sheet
Interviewers always ask for time and space complexity. Know the defaults cold so you can state them without thinking:
| Approach | Time | Space |
|---|---|---|
| Hash map lookup | O(n) | O(n) |
| Two pointers / sliding window | O(n) | O(1) |
| Binary search | O(log n) | O(1) |
| Sorting | O(n log n) | O(1) - O(n) |
| BFS / DFS on graph | O(V + E) | O(V) |
| Heap of size k | O(n log k) | O(k) |
| Backtracking (subsets) | O(2βΏ) | O(n) depth |
| DP (2D table) | O(nΒ·m) | O(nΒ·m) |
A study plan that works
Depth over breadth. Two focused weeks on patterns beat two months of random grinding:
- Week 1 - one pattern per day: learn the template, solve 3-4 problems, write the complexity from memory.
- Week 2 - mixed sets with no pattern label, so you practise recognition, not recall - exactly like the real thing.
- Ongoing - re-solve any problem you couldn't crack within a week; spaced repetition is what makes patterns stick.
Sources & further reading
- 1LeetCode - problem set & discussion β LeetCode
- 2NeetCode - patterns and curated roadmap β NeetCode
- 3Big-O cheat sheet β bigocheatsheet.com
- 4Cracking the Coding Interview β Gayle Laakmann McDowell