How to Think (the Interview Cheat Code)

The problem statement is full of clues that name the right pattern. Learn the decoder table and the 7-step routine, then practice the thinking itself on guided walkthroughs.

patternsinterviewproblem-solvingframework

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 forWhy it works
"sorted array", "find in sorted…"Binary searchSorted = you can throw away half per question
"minimum X such that it works", "max of min"Binary search on the answerFeasibility flips once → search the boundary
"pair / triplet that sums to…", "compare two ends"Two pointersEach comparison safely moves one pointer inward
"longest/shortest substring or subarray with…"Sliding windowGrow/shrink a range instead of re-scanning
"have I seen this before?", "count occurrences", "two things match"Hash tableTrades memory for instant lookups
"next greater element", "matching brackets", "undo", "most recent"StackLast-in first-out mirrors nesting & recency
"process in arrival order", "level by level", "shortest path, equal steps"Queue / BFSFirst-in first-out explores nearest-first
"top k", "k-th largest", "merge k streams", "median of a stream"HeapAlways hands you the current best in O(log n)
"all subsets / permutations / ways to place…"BacktrackingEnumerate by choose → explore → un-choose
"how many ways", "minimum cost to reach", "can it be formed" (+ choices overlap)DPSame sub-questions repeat → answer each once, in a table
"connected", "islands", "reachable", "depends on"Graph BFS/DFSRelationships = nodes + edges; traverse them
"are these in the same group?", "merge groups", "detect cycle while adding edges"Union-FindLeaders + sticky notes answer 'same group?' instantly
"intervals", "meetings", "overlapping ranges"Sort + sweepAfter sorting, only neighbours can interact
"prefix", "autocomplete", "starts with"TrieShared beginnings stored once, walked letter by letter
"range sum/min, many queries", "updates between queries"Prefix sums / FenwickPrecompute block answers; reuse forever
"linked list" + "cycle / middle / k-th from end"Fast & slow pointersDifferent 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.

  1. 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.
  2. 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).)
  3. Run the decoder. Which phrases above appear? Name 1–2 candidate patterns out loud — "all orderings, so this smells like backtracking."
  4. 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.
  5. 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.
  6. 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").
  7. Test like a skeptic. Trace the smallest real example, then one edge case from step 2. Fix quietly, don't panic loudly.
The honest version of 'I don't know'

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.

Think it through: Two SumEasy — LeetCode 10/5 stages

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. 1

    Restate & edges

    What am I actually returning, and what could be weird about the input?

  2. 2

    Brute force first

    What's the dumbest correct solution, and what does it cost?

    unlocks after the stage above
  3. 3

    Run the decoder

    'Have I seen this value before?' — which row of the table is that?

    unlocks after the stage above
  4. 4

    Code the template

    One pass, checking before storing — why that order?

    unlocks after the stage above
  5. 5

    Cost & edge check

    What did the hash map buy, and what did it cost?

    unlocks after the stage above
Think it through: Valid ParenthesesEasy — LeetCode 200/4 stages

PROBLEMGiven a string of brackets like '({[]})', decide if it's valid: every opener closes, in the right order. '([)]' is invalid.

  1. 1

    Solve it by hand

    Scan '({[]})' left to right with a pencil — what is my brain tracking?

  2. 2

    Run the decoder

    Which table row says 'most recent', 'matching brackets', 'nesting'?

    unlocks after the stage above
  3. 3

    Code the template

    What are the THREE ways a string can fail?

    unlocks after the stage above
  4. 4

    Cost & edge check

    Complexity, and which edge cases hit each failure mode?

    unlocks after the stage above
Think it through: Coin ChangeMedium — LeetCode 3220/5 stages

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. 1

    Trap check

    'Fewest coins' — why not just greedily grab the biggest coin every time?

  2. 2

    Run the decoder

    'Minimum number of X to reach Y', choices overlap — which row?

    unlocks after the stage above
  3. 3

    Define the table cell

    Complete the sentence: best[a] = … (this sentence is 80% of any DP problem)

    unlocks after the stage above
  4. 4

    Code it

    Fill the table smallest amount first — why that direction?

    unlocks after the stage above
  5. 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:

Dynamic programming — Coin changetime O(amount · coins)space O(amount)
01234567891011min
0
·
·
·
·
·
·
·
·
·
·
·

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".

problem

Drill the decoder

Check yourself0/4 answered

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?