25 patterns
Problem-solving / Patterns

The pattern playbook

Twenty-five patterns cover almost every LeetCode medium. Tier A — every interview asks one of these five. Tier B — you'll see at least two. Tier C — less frequent but specific enough that recognising them on sight saves the round. Each pattern has a "how to recognise it" check, canonical LC problems, reference implementation, and a five-problem easy-to-hard progression.


Tier A — universal (every interview asks one)

Master these five before anything else. Most LC mediums fall here.

  1. 01 · TIER A

    Two pointers

    Sorted array, pair-sum, palindromes, container-with-water. The default first guess on a sorted input.

    LC 167, 125, 11, 42
  2. 02 · TIER A

    Sliding window

    Variable or fixed window. Right edge grows, left edge shrinks. Substring problems are this pattern.

    LC 3, 76, 209, 239
  3. 03 · TIER A

    Hash map / set

    Counting, complement-lookup, dedupe. Trades memory for time. Turns the obvious O(n²) into O(n).

    LC 1, 49, 128, 560
  4. 04 · TIER A

    Binary search

    On a sorted array, or on the answer. The "binary search on the answer" half is the underappreciated one.

    LC 33, 153, 1011, 4
  5. 05 · TIER A

    Recursion & divide-and-conquer

    Trust the recursion. Tree traversal, merge sort, quickselect. Most tree problems collapse to three lines.

    LC 94, 215, 23, 226

Tier B — common (expect at least two)

The next five most-tested patterns. BFS, DFS, backtracking, DP, heap.

  1. 06 · TIER B

    BFS — graph & level-order

    Queue + visited. Unweighted shortest path; level-by-level traversal. Watch for the visited-set bug.

    LC 102, 200, 127, 994
  2. 07 · TIER B

    DFS — graph & components

    Stack or recursion. Connected components, cycle detection, topological sort. The other half of graph problems.

    LC 200, 207, 417, 79
  3. 08 · TIER B

    Backtracking

    Build candidate; recurse; undo. Permutations, combinations, N-Queens, Sudoku. The "try-all-cleverly" pattern.

    LC 46, 39, 51, 79
  4. 09 · TIER B

    Dynamic programming

    Memoise the recursion. State + transition. Knapsack, LCS, LIS, edit distance, DP-on-trees. The big one.

    LC 70, 198, 322, 72
  5. 10 · TIER B

    Heap / priority queue

    Top-K, k-way merge, running median. The structure that turns "find the largest" into O(n log k).

    LC 215, 23, 295, 347
  6. 21 · TIER B

    Segment tree & Fenwick (BIT)

    Range queries with point updates in O(log n). Lazy propagation for range updates. Fenwick tree as the lightweight cousin.

    LC 307, 315, 218

Tier C — pattern-specific (one in every three interviews)

Less frequent but recognisable on sight. Spot-train these once Tier A/B is fluent.

  1. 11 · TIER C

    Monotonic stack / queue

    Next greater element, sliding window max, histogram. The pattern with the funny name.

    LC 739, 84, 239
  2. 12 · TIER C

    Greedy

    Local optimum produces global. Hard to prove, easy to code wrong. Always articulate the exchange argument.

    LC 55, 134, 621, 767
  3. 13 · TIER C

    Union-Find (DSU)

    Connectivity queries, cycle detection in undirected graphs, Kruskal MST. Path compression + rank gives ~O(1).

    LC 323, 684, 721
  4. 14 · TIER C

    Trie

    Prefix-keyed lookup. Autocomplete, word-search-on-grid, replace-words. O(L) per op where L is key length.

    LC 208, 212, 648
  5. 15 · TIER C

    Topological sort

    Dependency ordering. Cycle detection in directed graphs. Kahn's algorithm or DFS-based.

    LC 207, 210, 269
  6. 16 · TIER C

    Bit manipulation

    XOR tricks, counting bits, subset enumeration. Niche, but when it lands, the cleanest solution.

    LC 136, 338, 78, 191
  7. 17 · TIER C

    Linked-list manipulation

    Slow/fast pointers, reverse, merge. Boring but exam-question-shaped. The LRU cache is here.

    LC 206, 141, 21, 146
  8. 18 · TIER C

    Tree DP

    DP on tree nodes — children compute up, parent decides. Diameter, max path sum, House Robber III.

    LC 543, 124, 337
  9. 19 · TIER C

    Graph algorithms

    Dijkstra, Bellman-Ford, Floyd-Warshall, Prim, Kruskal, Tarjan SCC. Less common in onsite, common in infra interviews.

    LC 743, 787, 1192
  10. 20 · TIER C

    Math / number theory

    GCD, LCM, modular arithmetic, prime sieves, fast exponentiation. Read the constraints; if they hint at math, it's math.

    LC 50, 69, 204, 171
  11. 22 · TIER C

    String matching — KMP, Z, rolling hash

    Substring search beyond O(nm). KMP failure function. Z-array. Polynomial rolling hash with double-hash for collision safety.

    LC 28, 214, 1392
  12. 23 · TIER C

    Advanced DP — bitmask, interval, digit, tree, CHT

    Beyond standard DP. Bitmask DP for N ≤ 20. Interval DP for partition problems. Digit DP for range counting. Tree DP with re-rooting.

    LC 847, 312, 233, 834
  13. 24 · TIER C

    Computational geometry — hull, sweep, intersection

    Orientation predicate. Convex hull via Andrew's monotone chain. Sweep line for skyline / interval problems. Point-in-polygon via ray casting.

    LC 587, 218, 149, 939
  14. 25 · TIER C

    Advanced graph — A*, Bellman-Ford, SCC, max-flow

    Beyond BFS/DFS/Dijkstra. Bellman-Ford for negative weights. A* with admissible heuristic. Tarjan SCC, bridges, max-flow / min-cut.

    LC 787, 1192, 332

How each page is structured

The same eight-section template. Drilling on one teaches the shape; reading five teaches what repeats.

  1. How to recognise it. The 2-minute decision rules. Sortedness, monotonicity, fixed alphabet — the cues.
  2. Canonical example. One representative LC problem walked end-to-end with timing and complexity.
  3. Reference implementation. Pseudocode plus a Python-ish reference. The shape that lives in muscle memory.
  4. Five-problem progression. Easy → medium → hard. Solve in order; each builds on the previous.
  5. Common pitfalls. The bugs that catch every candidate eventually.
  6. Complexity. Time and space, with the why.
  7. Variants & related patterns. What this pattern composes with.
  8. Further reading. Books, talks, related simulators on this site.

Suggested order

The order matters; each pattern builds on the previous. Don't pattern-hop.

PhasePatternsTime budget
Phase 1 — FoundationsTwo pointers, sliding window, hash map, binary search, recursion2–3 weeks
Phase 2 — Graphs & treesBFS, DFS, backtracking, tree DP2 weeks
Phase 3 — Heavy hittersDynamic programming, heap2 weeks
Phase 4 — Pattern depthMonotonic stack, greedy, union-find, trie, top-sort, bit, linked-list, graph algos, math3–4 weeks
Phase 5 — Mixed grindAll patterns, random selection, 25-min timer4 weeks