Start Here: The Pattern Decision Flowchart
Ask the questions in order — the first one that fits routes you to a short list of candidate patterns. All 20 patterns below appear exactly once.
Patterns 1–10: Array & String Patterns
01
Sliding Window Med–Hard
When: contiguous subarray/substring with a constraint on size or content
Signal words: "longest/shortest substring", "subarray with sum/at most k distinct"
Ex: #76 Min Window Substr, #239 Sliding Max, #424 Char Replace
02
Two Pointers Med–Hard
When: sorted array, palindrome check, container volume, sum pairs
Signal words: "sorted array", "pair that sums to", "in-place without extra space"
Ex: #42 Trapping Water, #15 3Sum, #11 Container
03
Fast & Slow Pointers Med
When: cycle detection, finding middle of linked list
Signal words: "detect cycle", "linked list", "circular array"
Ex: #141 LL Cycle, #142 Cycle II, #287 Find Duplicate
04
Merge Intervals Med–Hard
When: overlapping intervals, scheduling, coverage problems
Signal words: "intervals", "overlapping", "meetings", "scheduling"
Ex: #56 Merge Intervals, #57 Insert Interval, #253 Meeting Rooms
05
Cyclic Sort Med
When: array contains numbers in range [1,n] or [0,n-1], find missing/duplicate
Signal words: "find missing number", "duplicate in 1..n", "no extra space"
Ex: #268 Missing Number, #287 Find Duplicate, #448 Find All Missing
06
Monotonic Stack Hard
When: next greater/smaller element, span, histogram areas
Signal words: "next larger", "previous smaller", "span", "histogram"
Ex: #84 Histogram, #85 Max Rect, #739 Daily Temp
07
Prefix Sum Med
When: range sum queries, subarray sum equals k
Signal words: "subarray sum", "range sum", "contiguous sum equals"
Ex: #560 Subarray Sum=K, #974 Subarray Divisible K
08
Binary Search on Answer Hard
When: "minimum X such that condition holds" — condition is monotonic
Signal words: "minimum capacity", "minimum speed", "minimum days"
Ex: #410 Split Array, #875 Koko, #1011 Ship Packages
09
Top K Elements Med
When: find k largest/smallest/most frequent elements
Signal words: "top k", "k closest", "k most frequent"
Ex: #215 Kth Largest, #347 Top K Freq, #973 K Closest
10
K-way Merge Hard
When: merge k sorted arrays/lists, find kth element across sorted sources
Signal words: "k sorted lists", "merge k", "kth smallest in sorted matrix"
Ex: #23 Merge K Lists, #378 Kth Smallest Matrix
Patterns 11–20: Graph & DP Patterns
11
Topological Sort Med–Hard
When: dependency ordering, cycle detection in directed graphs
Signal words: "prerequisites", "build order", "dependency", "course schedule"
Ex: #207 Course Schedule, #210 Course Schedule II, #269 Alien Dict
12
Union Find Med–Hard
When: group elements, check connectivity, count components dynamically
Signal words: "connected components", "redundant edge", "groups"
Ex: #684 Redundant Connection, #200 Islands, #547 Provinces
13
Tree DFS Hard
When: path problems in tree, subtree aggregation, global + local result
Signal words: "path sum", "diameter", "root to leaf", "subtree"
Ex: #124 Max Path Sum, #543 Diameter, #297 Serialize
14
Tree BFS (Level Order) Med
When: level-by-level processing, shortest path in tree, zigzag
Signal words: "level order", "by level", "shortest path in tree", "zigzag"
Ex: #102 Level Order, #103 Zigzag, #116 Populate Next Ptr
15
DP on Strings Hard
When: compare two strings, edit operations, palindrome, subsequence
Signal words: "edit distance", "LCS", "palindromic", "distinct subsequences"
Ex: #72 Edit Dist, #1143 LCS, #115 Distinct Subseq, #516 LPS
16
0/1 Knapsack DP Hard
When: choose subset with constraint, partition into equal parts
Signal words: "subset with sum", "partition", "at most k items"
Ex: #416 Partition Equal Sum, #494 Target Sum, #474 Ones Zeros
17
State Machine DP Hard
When: multiple states (hold/no-hold, cooldown), transition between states
Signal words: "stock buy sell with cooldown", "at most k transactions"
Ex: #309 Cooldown, #123 Buy Sell III, #188 Buy Sell IV
18
Interval DP Hard
When: optimal cost to process a range, where order matters
Signal words: "burst", "remove", "matrix chain", "palindrome partition cost"
Ex: #312 Burst Balloons, #1000 Merge Stones, #546 Remove Boxes
19
Backtracking + Pruning Hard
When: generate all possibilities but with constraints that allow early exit
Signal words: "all combinations", "N-Queens", "Sudoku", "word search"
Ex: #37 Sudoku Solver, #51 N-Queens, #212 Word Search II
20
Dijkstra / BFS Shortest Path Hard
When: weighted graph shortest path, multi-state BFS, minimum cost
Signal words: "minimum cost to reach", "shortest path with states", "at most k stops"
Ex: #743 Network Delay, #787 Cheapest K Stops, #1631 Min Effort
Pattern recognition is the skill. You don't need to memorize 500+ solutions. You need to recognize which 1-3 patterns apply to a new problem. That maps a novel problem to a template you already know.
Recognition Speed Drill
Read each one-liner, name the pattern out loud before revealing. Target: under 15 seconds each. This is the exact skill the flowchart above trains.
1Find the median of a live stream of numbers at any moment.▼
Top K Elements — two-heaps variant. "Stream" + "median" = maintain a max-heap of the lower half and a min-heap of the upper half; rebalance so sizes differ by ≤ 1.
2Longest substring containing at most 2 distinct characters.▼
Sliding Window. "Longest substring" + a content constraint that's monotone (more chars can only add distinct letters) — expand right, shrink left when distinct > 2.
3Given course prerequisites, can you finish every course?▼
Topological Sort. "Prerequisites"/"dependencies" = DAG. Finishable ⟺ no cycle ⟺ topological order includes all nodes (Kahn's: processed count == V).
4Slowest eating speed that still finishes all banana piles in h hours.▼
Binary Search on Answer. "Minimum speed such that…" — feasibility is monotone in speed (faster always still works), so binary search the answer space, not the array.
5Merge 8 sorted log files into one sorted output.▼
K-way Merge. K sorted sources → min-heap of one head per source; pop the smallest, push its successor. O(N log K).
6Count the islands in a grid of land and water cells.▼
Grid DFS/BFS (Tree DFS family) — or Union Find. "Connected regions in a grid" = flood fill from each unvisited land cell. Union Find wins when islands merge dynamically over time.
7Max stock profit when every sell forces a 1-day cooldown.▼
State Machine DP. "Best sequence of buy/sell decisions with rules between states" — states {holding, just-sold, resting} with transitions per day.
8Return every valid IP address you can form from a digit string.▼
Backtracking + Pruning. "Return ALL valid…" = enumerate with constraints. Prune: each octet ≤ 255, no leading zeros, remaining digits must fit remaining octets.