Why patterns beat random grinding
LeetCode has 3,000+ problems. Nobody solves all of them. Top engineers solve 150–300 problems but pick them strategically — one representative problem per sub-pattern until the pattern is internalized. The insight: every "new" problem you see is almost always a combination of 2–3 patterns you already know. Learn the 21 patterns and you have a mental library to draw from.
Array patterns (1–5)
1. Two Pointers — converging on sorted arrays, same-direction for in-place ops, fast/slow for cycles. Signals: sorted array, in-place, "pair sum". 2. Sliding Window — variable or fixed window on arrays/strings. Signals: longest/shortest subarray, "at most k distinct". 3. Prefix Sum — precompute cumulative sums for O(1) range queries. Signals: "sum of subarray", "count subarrays with sum = k". 4. Binary Search — not just on sorted arrays, also on answer space. Signals: sorted data, "minimum X satisfying condition". 5. Merge Intervals — sort by start, merge overlapping. Signals: intervals, "overlapping meetings", "free time".
Linked list and stack patterns (6–9)
6. Linked List In-Place — reversal, slow/fast pointer, merge. No extra memory allowed signals this. 7. Stack — monotonic stack for next greater/smaller element problems. Signals: "next greater", "largest rectangle", "daily temperatures". 8. Monotonic Queue — deque for sliding window maximum/minimum. Signals: sliding window + max/min per window. 9. Hash Map / Set — frequency counting, "seen before", grouping. O(1) lookup makes brute-force O(n²) become O(n).
Tree and graph patterns (10–14)
10. Tree DFS — recursion, all traversal orders. 11. Tree BFS — queue, level-order. 12. Graph BFS — shortest path, multi-source flood fill. 13. Graph DFS — path existence, cycle detection, topological sort. 14. Union-Find — connected components, cycle in undirected graph, merging groups dynamically.
Advanced patterns (15–21)
15. Heap / Priority Queue — top-K elements, K closest, merge K sorted. Signals: "kth largest", "top K", "K closest". 16. Trie — prefix search, autocomplete, word search. 17. Backtracking — permutations, combinations, N-Queens. Prune the search tree aggressively. 18. Dynamic Programming — optimal substructure + overlapping subproblems. 19. Greedy — local optimum leads to global optimum. Signals: interval scheduling, gas station. 20. Bit Manipulation — XOR for find-the-missing/unique, bit masks for subsets, shifts for powers of 2. 21. Math / Number Theory — GCD, prime sieve, modular arithmetic, combinatorics. Appears in hard problems at Google.
How to study the patterns
For each pattern: (1) Read the pattern template and understand the invariant. (2) Solve 3 easy problems to build muscle memory. (3) Solve 5 medium problems — these are interview-level. (4) Solve 1–2 hard problems to understand the ceiling. (5) Without notes, write the template from memory. You do not need to solve every problem — you need to reach the point where you can identify the pattern within 60 seconds of reading a problem statement.