Pattern Recognition
The hard part of an interview is not implementing the solution — it's knowing which pattern the problem is asking for. This is the routing table: a systematic decision framework of signals, constraints, and traps so you never freeze at the whiteboard.
Recognition is the real skill
You can drill seventeen implementations to muscle memory and still lose the interview in the first five minutes — by picking the wrong one. Recognition is a separate skill, and it can be practiced separately: read problems and name the pattern without coding.
The master decision tree
Start from the primary data structure and walk down. Each branch is one question.
Keyword → pattern
Problems telegraph their pattern through specific words. This table maps the signals you'll actually read to the pattern you should reach for — the last column links to the guide that covers it.
| Signal / keyword | Pattern | Time | Guide |
|---|---|---|---|
| “sorted array”, “find target” | Binary Search | O(log n) | 06 |
| “two numbers that sum to” | Two Pointers / Map | O(n) | 01, 02 |
| “longest/shortest substring with…” | Sliding Window | O(n) | 05 |
| “subarray sum equals k” | Prefix Sum + Map | O(n) | 09 |
| “merge overlapping intervals” | Sort + Sweep | O(n log n) | 13 |
| “top k”, “kth largest/smallest” | Heap | O(n log k) | 12 |
| “next greater element” | Monotonic Stack | O(n) | 14 |
| “all combinations/subsets/permutations” | Backtracking | O(2ⁿ) or O(n!) | 11 |
| “minimum cost”, “maximum profit”, “ways” | Dynamic Programming | varies | 07 |
| “connected components”, “union” | Union-Find | O(n · α(n)) | 17 |
| “course schedule”, “build order” | Topological Sort | O(V + E) | 03 |
| “shortest path”, “minimum steps” | BFS | O(V + E) | 04 |
| “word search”, “prefix”, “autocomplete” | Trie | O(m) | 16 |
| “valid parentheses”, “calculator” | Stack | O(n) | 01 |
| “linked list cycle”, “middle node” | Fast & Slow Pointers | O(n) | 15 |
| “matrix”, “grid”, “island” | BFS/DFS on grid | O(R · C) | 03, 04 |
| “meeting rooms”, “min platforms” | Sort + Heap/Sweep | O(n log n) | 12, 13 |
| “palindrome” | Two Pointers or DP | O(n) or O(n²) | 02, 07 |
| “LRU Cache” | HashMap + DLL | O(1) | special |
| “design”, “implement” | Multiple DS combined | varies | varies |
The constraints tell you the algorithm
The second signal is numeric. The size of n tells you what complexity the
problem-setter expects — which shortlists the patterns that can deliver it.
| Constraint | Expected complexity |
|---|---|
| n ≤ 10 | O(n!) — brute force / backtracking |
| n ≤ 20 | O(2ⁿ) — bitmask / backtracking |
| n ≤ 100 | O(n³) — triple loop / DP |
| n ≤ 1,000 | O(n²) — DP / nested loop |
| n ≤ 10,000 | O(n · √n) |
| n ≤ 100,000 | O(n log n) — sort / heap |
| n ≤ 1,000,000 | O(n) — linear scan |
| n ≤ 10⁹ | O(log n) — binary search |
Classifying a problem, live
Here's the framework applied to a real statement — the single most-missed classification in the trap table. Read the signals, put the candidates on the board, and eliminate until one survives.
“Obvious but wrong” pattern choices
These are the problems where the loudest signal points at the wrong pattern. Learn the table, then the four biggest offenders in detail.
| Problem description | Tempting | Correct | Why |
|---|---|---|---|
| “Find if path exists” in weighted graph | BFS | Dijkstra's | BFS doesn't handle weights |
| “Longest substring without repeating” | Two pointers | Sliding Window | Need to shrink window dynamically |
| “Subarray sum = k” (with negatives) | Sliding Window | Prefix Sum + HashMap | Window can't shrink with negatives |
| “Number of islands” (follow-up: dynamic) | DFS | Union-Find | Dynamic additions need incremental updates |
| “Word break” (can string be segmented?) | Greedy / Trie only | DP | Greedy fails: “catsanddog” |
| “Jump Game II” (minimum jumps) | DFS / Backtracking | Greedy / BFS | BFS by levels gives min jumps |
| “Valid parentheses” with multiple types | Counter | Stack | Counter can't track ordering |
| “Merge k sorted lists” | Merge one by one | Min Heap | One-by-one is O(nk), heap O(n log k) |
| “Course schedule” (can finish all?) | BFS on adj list | Topological Sort | Need to detect cycles in DAG |
"catsanddog" — committing to "cats" vs "cat" changes what's
reachable later. Overlapping subproblems + a yes/no over prefixes = DP.
"([)]" has matched
counts but invalid nesting. Multiple bracket types need a stack.
Decision flowcharts for the crowded cases
Three problem families where several patterns plausibly apply. Walk the chart instead of guessing.
F1Subarray problems
F2Graph problems
F3DP problems
The one-glance card
Every pattern, its key signal, and its cost. This is the table to be able to reproduce from memory.
| Pattern | Key signal | Time | Space |
|---|---|---|---|
| Hash Map | Count / lookup / group | O(n) | O(n) |
| Two Pointers | Sorted + pair/triplet | O(n) | O(1) |
| Sliding Window | Contiguous + constraint | O(n) | O(k) |
| Prefix Sum | Range sum / subarray sum | O(n) | O(n) |
| Binary Search | Sorted + find target | O(log n) | O(1) |
| Merge Intervals | Overlapping intervals | O(n log n) | O(n) |
| DFS/BFS (graph/tree) | Traversal / shortest | O(V+E) | O(V) |
| Topological Sort | Dependencies / ordering | O(V+E) | O(V+E) |
| Heap | Top K / merge K sorted | O(n log k) | O(k) |
| Monotonic Stack | Next greater/smaller | O(n) | O(n) |
| Backtracking | All combos / subsets | O(2ⁿ) | O(n) |
| DP | Optimize + subproblems | varies | varies |
| Trie | Prefix search / words | O(m) | O(n·m) |
| Union-Find | Connectivity / groups | O(α(n)) | O(n) |
| Fast & Slow Pointers | Cycle / middle / nth | O(n) | O(1) |
Multi-pattern problems
Some problems admit several valid approaches — knowing the alternatives lets you pick the one you code fastest, and gives you a follow-up answer when the interviewer asks “any other way?”
| Problem | Valid approaches |
|---|---|
| Number of Islands | DFS, BFS, Union-Find |
| Longest Consecutive Sequence | Hash Set, Union-Find |
| Course Schedule | BFS (Kahn's), DFS |
| Word Break | DP, BFS, Trie + DP |
| Kth Largest Element | Heap, Quickselect |
| Palindrome Partitioning | Backtracking, DP |
| House Robber | DP array, DP variables |
The 35-minute game plan
Recognition happens in the first ten minutes — protect that time. Do not start coding early, and do not go silent.
If you're stuck
- Talk through examples out loud.
- Think about brute force first — what would O(n²) look like?
- Ask: “What data structure gives me the lookup I need?”
- Ask: “Have I seen a similar problem?” — map it to a known pattern.
- Look at the constraints for the complexity hint (section 03).
- Ask the interviewer: “Would it help if I sorted the input first?”
Practice ladder
In this order — easiest first. Tick each one as you solve it, and re-solve anything that took more than 25 minutes two days later.