The secret nobody tells beginners
Interviewers aren't testing whether you've memorized 500 solutions. They're testing whether you can recognize which of ~15 patterns a new problem is wearing, and then apply that pattern calmly. Strong candidates aren't faster thinkers — they've just learned that the problem statement itself names the pattern, if you know the code words.
This page is that decoder. Pin it.
The decoder table
Read the problem. The moment one of these phrases appears, you have your prime suspect:
| The problem says… | Reach for | Why it works |
|---|---|---|
| "sorted array", "find in sorted…" | Binary search | Sorted = you can throw away half per question |
| "minimum X such that it works", "max of min" | Binary search on the answer | Feasibility flips once → search the boundary |
| "pair / triplet that sums to…", "compare two ends" | Two pointers | Each comparison safely moves one pointer inward |
| "longest/shortest substring or subarray with…" | Sliding window | Grow/shrink a range instead of re-scanning |
| "have I seen this before?", "count occurrences", "two things match" | Hash table | Trades memory for instant lookups |
| "next greater element", "matching brackets", "undo", "most recent" | Stack | Last-in first-out mirrors nesting & recency |
| "process in arrival order", "level by level", "shortest path, equal steps" | Queue / BFS | First-in first-out explores nearest-first |
| "top k", "k-th largest", "merge k streams", "median of a stream" | Heap | Always hands you the current best in O(log n) |
| "all subsets / permutations / ways to place…" | Backtracking | Enumerate by choose → explore → un-choose |
| "how many ways", "minimum cost to reach", "can it be formed" (+ choices overlap) | DP | Same sub-questions repeat → answer each once, in a table |
| "connected", "islands", "reachable", "depends on" | Graph BFS/DFS | Relationships = nodes + edges; traverse them |
| "are these in the same group?", "merge groups", "detect cycle while adding edges" | Union-Find | Leaders + sticky notes answer 'same group?' instantly |
| "intervals", "meetings", "overlapping ranges" | Sort + sweep | After sorting, only neighbours can interact |
| "prefix", "autocomplete", "starts with" | Trie | Shared beginnings stored once, walked letter by letter |
| "range sum/min, many queries", "updates between queries" | Prefix sums / Fenwick | Precompute block answers; reuse forever |
| "linked list" + "cycle / middle / k-th from end" | Fast & slow pointers | Different speeds reveal structure without extra memory |
Two suspects at once is normal ("sorted" + "pair" → two pointers on a sorted array). The table narrows fifteen options to two — your worked examples settle the rest.
The 7-step routine
Same moves, every problem, out loud. Interviewers grade the moves as much as the code.
- Restate the problem in your own words. One sentence. If you can't, you don't understand it yet — and discovering that now costs nothing.
- Interrogate the edges. Empty input? Duplicates? Negative numbers? Sorted already? How big is n? (n ≤ 15 whispers backtracking is fine; n = 10⁵ shouts nothing slower than O(n log n).)
- Run the decoder. Which phrases above appear? Name 1–2 candidate patterns out loud — "all orderings, so this smells like backtracking."
- Solve it by hand first. Take a 5-element example and get the answer with pencil logic. Watch what your own brain does — your hand-method usually is the algorithm.
- State the brute force and its cost. Twenty seconds: "check every pair, O(n²)." Now you have a baseline, and improving it has direction: what work is being repeated? Repeated lookups → hash. Repeated halves → binary search. Repeated sub-answers → DP.
- Code the chosen pattern's template. Not from inspiration — from the template you drilled. Say the invariant while you type ("window is always valid after this while-loop").
- Test like a skeptic. Trace the smallest real example, then one edge case from step 2. Fix quietly, don't panic loudly.
Stuck after the decoder? Say: "Brute force is O(n²); I'm looking for the repeated work to eliminate." That sentence — naming the baseline and the search direction — earns more points than silent staring ever lost.
Practice the thinking, not the typing
Each walkthrough below hides the thinking behind the same questions you should ask yourself. Answer each prompt in your head before revealing — that rep, not the final code, is what transfers to a new problem.
PROBLEMGiven an array of numbers and a target, return the indices of the two numbers that add up to the target. Example: nums = [2, 7, 11, 15], target = 9 → [0, 1].
- 1
Restate & edges
“What am I actually returning, and what could be weird about the input?”
- 2
Brute force first
“What's the dumbest correct solution, and what does it cost?”
unlocks after the stage above - 3
Run the decoder
“'Have I seen this value before?' — which row of the table is that?”
unlocks after the stage above - 4
Code the template
“One pass, checking before storing — why that order?”
unlocks after the stage above - 5
Cost & edge check
“What did the hash map buy, and what did it cost?”
unlocks after the stage above
PROBLEMGiven a string of brackets like '({[]})', decide if it's valid: every opener closes, in the right order. '([)]' is invalid.
- 1
Solve it by hand
“Scan '({[]})' left to right with a pencil — what is my brain tracking?”
- 2
Run the decoder
“Which table row says 'most recent', 'matching brackets', 'nesting'?”
unlocks after the stage above - 3
Code the template
“What are the THREE ways a string can fail?”
unlocks after the stage above - 4
Cost & edge check
“Complexity, and which edge cases hit each failure mode?”
unlocks after the stage above
PROBLEMCoins of denominations [1, 3, 4] and an amount, say 6. Return the FEWEST coins that make the amount, or -1 if impossible. (Answer for 6: two coins — 3 + 3.)
- 1
Trap check
“'Fewest coins' — why not just greedily grab the biggest coin every time?”
- 2
Run the decoder
“'Minimum number of X to reach Y', choices overlap — which row?”
unlocks after the stage above - 3
Define the table cell
“Complete the sentence: best[a] = … (this sentence is 80% of any DP problem)”
unlocks after the stage above - 4
Code it
“Fill the table smallest amount first — why that direction?”
unlocks after the stage above - 5
Cost & edge check
“Cost in terms of amount and number of coin types? Edge cases?”
unlocks after the stage above
Want to watch that last table fill itself? Same problem, cell by cell:
1/41Fewest coins to pay each amount from 0 to 11, using coins {1, 2, 5}. Amount 0 needs 0 coins; "·" means "no way found yet".
Drill the decoder
1. “Return the k closest points to the origin from a stream of 10⁶ points.” First pattern to consider?
2. “Count how many distinct ways you can decode a digit string.” Which pattern, and which clue?
3. “Find the longest substring with at most 2 distinct characters.” Which pattern?
4. You're 10 minutes in with no idea. What's the highest-value move?