Which Pattern? The Decision Engine
The interviewer finishes reading the problem and goes quiet. You have about 30 seconds to say something intelligent — and what you say in those 30 seconds decides the next 40 minutes. Candidates who pick the right pattern early spend the round coding; candidates who pick wrong spend it digging out. This module is the capstone: no new patterns, just the skill of choosing between the eleven you already have.
The Decision Tree
This is the questioning sequence to run in your head — the same questions, in the same order, every problem. Read it top to bottom now; by the end of this module it should run without you noticing.
flowchart TD
START["Read the problem twice"] --> Q0{"Is the input a linked list?"}
Q0 -->|yes| Q0A{"Cycle or middle in one pass?"}
Q0A -->|yes| FS["Fast and slow pointers"]
Q0A -->|no| LL["In place pointer rewiring"]
Q0 -->|no| Q1{"Array or string input?"}
Q1 -->|yes| Q2{"Is it sorted or can you sort it cheaply?"}
Q2 -->|yes| Q3{"Looking for a pair or rearranging in place?"}
Q3 -->|yes| TP["Two Pointers"]
Q3 -->|no| BS["Binary Search"]
Q2 -->|no| Q4{"Contiguous subarray or substring?"}
Q4 -->|yes| SW["Sliding Window"]
Q4 -->|no| Q5{"Lookups counts or duplicates?"}
Q5 -->|yes| HM["Hash Map"]
Q5 -->|no| Q7
Q1 -->|no| Q6{"Nested structure or most recent first?"}
Q6 -->|yes| ST["Stack"]
Q6 -->|no| Q7{"Top k or kth largest or smallest?"}
Q7 -->|yes| HP["Heap"]
Q7 -->|no| Q8{"Generate ALL combinations or permutations?"}
Q8 -->|yes| BT["Backtracking"]
Q8 -->|no| Q9{"Overlapping subproblems plus an optimal value?"}
Q9 -->|yes| DP["Dynamic Programming"]
Q9 -->|no| Q10{"Graph grid or tree?"}
Q10 -->|level order or shortest hops| BFS["BFS"]
Q10 -->|explore every path| DFS["DFS"]
Q10 -->|neither| BF["Brute force then optimize"]
Three things about using this tree:
- The questions are about the problem’s shape, not its story. Bananas, stock prices, meeting rooms — costumes. Strip the story, keep the structure: what’s the input, is it ordered, what’s being asked for.
- Two questions do most of the work. “Is it sorted?” and “is it contiguous?” eliminate half the tree instantly. Ask them first, always.
- The tree gives you a hypothesis, not a verdict. You still walk one example through by hand (step 3 of the protocol) before writing code. If the walk-through feels forced, climb back up the tree.
The Trigger-Word Table
Problems telegraph their pattern in specific phrases. Learn these the way you learned that a NullPointerException means “follow the reference” — instant, no thinking:
| When the problem says… | Reach for | Why |
|---|---|---|
| ”sorted array” | Two pointers or binary search | Order is information; not using it means you are solving a harder problem than the one asked |
| ”find a pair that sums to” | Hash map (unsorted) / two pointers (sorted) | One pass with complements, or walk inward from both ends |
| ”contiguous subarray” | Sliding window | Contiguity means the next window reuses almost all of the last one |
| ”longest substring with…” | Sliding window | Longest plus a breakable condition equals grow right, shrink left |
| ”top k” or “k most frequent” | Heap | A size-k min-heap beats sorting everything: O of n log k vs O of n log n |
| ”kth largest” or “kth smallest” | Heap | Same trick; you never need the full sorted order, only the boundary |
| ”valid parentheses” or “matching pairs” | Stack | The most recent opener must close first — last in, first out |
| ”next greater element” | Monotonic stack | Hold unresolved items; resolve them the moment a bigger one arrives |
| ”undo” or “most recent first” | Stack | The data structure IS the requirement |
| ”minimum number of ways” or “fewest coins” | DP | Optimal value built from overlapping smaller answers |
| ”how many ways to…” | DP | Counting paths through repeated subproblems |
| ”all combinations” or “all permutations” or “generate all” | Backtracking | The answer is the full set of choices — build, recurse, undo |
| ”in place” or “O(1) extra space” | Two pointers | The space limit bans the easy hash-map or copy answer |
| ”have we seen this before” or “duplicate” or “count occurrences” | Hash map | O(1) membership and counting is the whole point of hashing |
| ”shortest path” (unweighted) or “level by level” | BFS | BFS visits in rings of distance — the first arrival IS the shortest |
| ”all paths” or “connected regions” or “islands” | DFS | Go deep, exhaust a branch, backtrack — floods a region completely |
| ”cycle in a linked list” or “middle of the list” | Fast and slow pointers | Two speeds on one chain: they meet in a loop, fast hits the end when slow hits the middle |
| ”minimize the largest…” or “smallest value that still works” | Binary search on the answer | If a value works, everything above it works — that yes/no boundary is searchable |
Notice the last row: binary search doesn’t need an array. It needs a monotonic condition — once true, always true as you increase the value. That re-frame is worth more interview points than any single problem.
The Mega Quiz
Fifteen problems, all eleven patterns, mixed difficulty — and three of them are traps that sound like one pattern and are actually another. These are real famous problems; you’ll meet most of them again on LeetCode. Pattern only. Don’t solve — recognize.
Scoring honestly: 12+ — your recognition is interview-grade, keep it warm with weekly mixed sets. 8–11 — solid, but re-drill the patterns you missed (go back to that module’s Spot The Pattern quiz). Under 8 — the patterns aren’t automatic yet; that’s information, not failure. Re-run the modules you missed and come back in a week.
When No Pattern Fits
Sometimes you run the whole tree and nothing clicks. This is fine. It happens in interviews and it happens in real tickets, and there’s a professional move for it:
Say the brute force out loud, then optimize it. “The brute force checks every pair, which is O of n squared. Let me start there and look for the repeated work.” This is not a confession of weakness — it’s the strongest move available, for three reasons:
- It’s a baseline. You now have a working answer and a complexity to beat. Interviewers pass candidates who ship a brute force and analyse it, and fail candidates who sit silent hunting for the clever answer.
- The optimization usually reveals the pattern. Ask of your brute force: what work am I repeating? Recomputing lookups → hash map. Re-scanning ranges → sliding window. Re-solving subproblems → DP. The waste points at the fix.
- It’s how real engineering works. Nobody at a fintech rewrites a slow report by staring at it until inspiration strikes. They profile it, find the repeated work, and remove it. Brute-force-then-optimize IS the job.
Brute force is allowed. Staying silent is not.
Check Yourself
- What are the first two questions of the decision tree that eliminate the most options, and why those two?
- A problem gives you a sorted array. Which two patterns are in play, and what follow-up question decides between them?
- Why does “fewest” or “minimum number of ways” suggest DP and not greedy? Give the coin counter-example.
- A linked list problem demands O(1) extra space. What does that constraint rule out, and what does it point to?
- “Find the smallest speed that still finishes in time” — which pattern, and what property of the problem makes it work?
- You’ve run the whole tree and nothing fits. What do you say in the interview, word for word?
Answers
- “Is it sorted?” and “is it contiguous?” Sorted routes you to two pointers / binary search; contiguous routes to sliding window. Between them they cover the majority of array and string problems, so they cut the search space fastest.
- Two pointers and binary search. The follow-up: am I looking for a pair (or rearranging in place) → two pointers; am I looking for one value or a boundary → binary search.
- Greedy commits to the locally best choice and can’t undo it. With coins 1, 3, 4 and target 6, greedy takes 4 + 1 + 1 (three coins) but the optimum is 3 + 3 (two). Overlapping subproblems plus an optimal value means you need DP’s “best of all smaller answers.”
- It rules out the hash-set “have I visited this node” answer, which costs O(n) space. It points to fast and slow pointers — two speeds on the same chain, constant space.
- Binary search on the answer space. It works because the condition is monotonic: if speed s finishes in time, every speed above s does too, so there’s a single yes/no boundary to find.
- “The brute force is [X], which costs [complexity]. Let me start there and look for the repeated work to optimize.” Baseline first, pattern second.
Explain it out loud: Take any problem from the mega quiz you got wrong. Explain to an empty chair why the wrong answer was tempting, what the tell was, and how the decision tree routes to the right pattern. If you can explain the trap, you won’t fall in it twice.
Why AI Can’t Do This For You
Paste any of those 15 problems into Claude and it answers instantly — pattern, code, complexity, done. But in the interview room there is no paste. There is you, a problem you’ve never seen, and 30 seconds of silence to fill with something intelligent. The decision tree has to run in your head, because that room is the one place the tool isn’t.
And it’s not just interviews. Real work hands you a vague ticket: “the transaction history page is slow for old accounts.” Nobody labels it “binary search problem” — recognizing that an ordered table plus a date filter is a searchable boundary, that repeated lookups want a cache, that the report recomputes the same subtotals — that’s pattern recognition on problems that never arrive in LeetCode costume. AI can write the fix once you’ve named the problem. Naming it is the job.
Module done? Add it to today’s tracker