{} Coding Interviews · ch.02 · the solve framework
🧩 Part I · The Game · chapter 2 / 24

Six steps,
zero silence

Every problem in the room gets the same treatment: restate, small example, brute force priced, pattern named, skeleton coded, trace and test. When your mind blanks, the loop keeps moving — even while you aren't.

Chapter 1 showed what the interviewer is scoring: visible problem-solving, not a silent right answer. This chapter is the machine that produces that visibility on demand — a six-step loop you run on every problem, easy or hard, whether or not you recognize it. The loop is not a crutch for weak candidates; it's what strong candidates are doing when they look effortless.

The point of a fixed procedure is that it survives adrenaline. You will forget clever things in the room. You will not forget a loop you've run two hundred times in practice — and every step of it emits exactly the signal the four dials from Chapter 1 are listening for. Chapter 24 will put this same loop under a 35-minute clock; today we install it.

1One loop for every problem — the solve framework

Here it is. Six steps, in order, no skipping:

  • 1 · Restate & clarify. Say the problem back in your own words; pin down inputs, outputs, and constraints.
  • 2 · Work a small example by hand. Five-ish elements, plus one nasty case. Paper before keyboard.
  • 3 · Brute force, out loud, with its cost. "The naive way is X, and it's O(n²)." That sentence is your safety net.
  • 4 · Find the structure, name the pattern. What's being re-computed? Is anything sorted, counted, nested? This is where Chapters 4–22 plug in.
  • 5 · Code the skeleton. The memorized shape first, local details second, narrating as you type.
  • 6 · Trace & test. Run your small example through your own code, line by line, then the edges — before declaring done.

Notice each step leaves behind an artifact — a clarified constraint, a worked example, a priced brute force — that later steps cash in. The walkthrough below runs all six on Valid Anagram Easy and shows you exactly what gets banked at each stop.

Interactive · framework walkthrough Step the six-stage loop through Valid Anagram; watch the artifacts pile up
step 0 / 6 — the problem
current step
read the problem
minute mark (of ~35)
0
artifacts banked
0 / 6
💡
The loop front-loads the cheap steps. Steps 1–4 cost about ten minutes and no code — but they decide everything about the code. Most blown interviews die in minute two, when the candidate skips straight to step 5 on a problem they haven't actually read.

2Say it back — restate & clarify

Step 1 has two halves. The restate half is for you: "So we're given two strings and we return true when they contain the same letters with the same counts — order ignored?" If you can't say it, you can't solve it; if you say it wrong, the interviewer corrects you now, for free, instead of twenty minutes into the wrong program.

The clarify half is an interrogation, and it's not politeness theater — the answers change the solution. Problem statements are deliberately underspecified, and hiding in the gaps are decisions about your data structures. Is the input sorted? (Sorted whispers two pointers, Chapter 5.) Can there be duplicates? How large is n? (That number sets your complexity budget — Chapter 3 is entirely about reading it.) And the classic:

🎯
The tell: "return the indices" vs "return the values" changes the whole data structure. Sorting scrambles indices, so an indices answer quietly bans sort-based approaches and forces your hash map to store value → index. One clarifying word, entire architecture — ask before you code.

Not all questions are equal, though. "Can values be negative?" usually changes nothing; "can I reuse an element?" changes the order of two lines and prevents a real bug. The drill below hands you an ambiguous Two Sum Easy statement and eight candidate questions — find the five that actually change the solution.

Interactive · clarifying-question picker Eight questions, five of which change the solution — ask and find out
solution-changers found
0 / 5
cosmetic questions spent
0
clock used
0:00

3Shrink it until it fits in your head — the worked example

Step 2 is the one experienced engineers skip most, because it feels beneath them. It isn't. Working nums = [2, 7, 11, 15], target = 9 by hand does three jobs at once: it verifies your restatement against reality, it produces the test case you'll trace in step 6, and — this is the sneaky one — watching your own hands solve it often reveals the algorithm. When you catch yourself thinking "9 minus 2 is 7, have I seen a 7?", you've just discovered the hash-map solution out loud. Your hands knew the pattern before your head did.

Build the example adversarially. For Valid Anagram Easy, the pair "car" / "rac" confirms the happy path — but "aab" / "abb" is the one that earns its keep, because it kills the tempting-but-wrong "same set of letters" idea before it reaches your code. One vanilla case, one case designed to break your first guess.

💡
The example is also your communication anchor. Every later sentence — "so at this point the map holds {2: 0}…" — points at something concrete the interviewer can see. Abstract narration is hard to follow; narration about this array right here is easy to score.

4The ugly answer first — brute force, priced

Step 3 feels embarrassing and is actually a power move. Saying "the obvious approach is to check every pair — that's O(n²) — let me see if there's structure to beat it" accomplishes four things in one breath: it proves you can solve the problem (worst case, you code this and pass), it names a cost — instant credit on the problem-solving dial from Chapter 1 — it sets up the improvement story, and it buys you legal thinking time. Silence while you hunt for the clever answer reads as stuck; a priced brute force reads as methodical.

The price tag is mandatory. "I could brute-force it" is a shrug; "brute force is O(n²) — n is 10⁵, so that's 10¹⁰ operations, too slow" is analysis. It also tells you exactly how much better you need to be, which narrows the pattern search in step 4: needing to beat O(n²) on an unsorted array is practically a formal invitation to the hash map.

🎯
The tell: the constraints line — "1 ≤ n ≤ 10⁵" — is the interviewer telling you which complexities are allowed to survive. Read it during step 1, cash it in during steps 3–4. Chapter 3 turns this into a full elimination system; for now: if your brute force fits the budget, say so and ship it.
⚠️
The perfectionist trap: refusing to mention the brute force because it isn't clever. Interviewers regularly watch candidates sit on a workable O(n²) for ten silent minutes, hunting for elegance, and end with nothing on the board. The brute force is not your final answer — it's your floor, and floors are stated, not hidden.

5Find the structure — name the pattern

Step 4 is where this book's whole thesis reports for duty. You're not inventing an algorithm; you're running recognition. Interrogate what you've already banked:

  • What does the brute force waste? Re-scanning for something you've already walked past → remember it in a hash map (ch. 4). Re-computing a range total → prefix sums (ch. 7). Re-solving the same subproblem → DP (ch. 20).
  • What's the input's shape? Sorted → two pointers or binary search (ch. 5, 9). Contiguous "longest/shortest substring" → sliding window (ch. 6). Nested or matched → a stack (ch. 8). A grid of regions → it's a graph (ch. 15).
  • What did your restatement literally say? Words like counts, seen before, pair that sums are pattern names wearing casual clothes.

For Valid Anagram, the restatement did all the work: "same letters with the same counts." Counting things is a frequency map — the bread and butter of Chapter 4. Say the name out loud: "this is a counting problem; I'll use a hash map, which makes it O(n) — one pass to count up, one to count down." Naming the pattern before coding it is among the strongest signals an interviewer can write down.

🎯
The tell: your own restatement is a tell generator. If saying the problem back forces words like "count how many", "have I seen", "longest run of", or "in order of dependency" out of your mouth, the pattern just introduced itself. This is why step 1 is not optional — Chapter 23 builds a whole decision tree out of exactly these phrases.

6Skeleton first, details second — code, then trace

Step 5: write the pattern's memorized shape before any problem-specific cleverness. For a counting problem that's "guard on lengths, count up over one input, count down over the other, verdict" — four beats you can type without thinking, narrating each ("length guard first; that's the constraint we clarified"). The skeleton-first habit is why every pattern chapter in this book ends section 8 with a whiteboard skeleton: those 6–8 lines are the thing you reproduce here, under pressure, while your remaining attention handles the local details.

Step 6: your code is a claim, and claims get tested. Take the small example from step 2 and trace it through the code you actually wrote — not the code you meant to write — mumbling state as you go: "counts is {a:2, b:1}, now t's second b takes b to −1, return false. Correct." Then the edges you banked in step 1: empty strings, mismatched lengths, duplicates. Only then: "I'm confident in this — it's O(n) time, O(1) space for a fixed alphabet." That closing sentence is the verification dial's favorite food.

⚠️
The "looks done" trap: announcing "that should work" the instant the last line is typed. Untested code that happens to be right scores worse than tested code with a caught-and-fixed bug — the second one produced evidence of testing instinct, the first produced evidence of hope. Finding your own bug in step 6 is not a stumble; it's the show.

7When the gears jam — the unstick moves

You will get stuck. The interviewer knows you will get stuck; some problems are chosen so that everyone does. Stuck is not the failure state — silent stuck is. The difference between candidates who recover and candidates who spiral is that the recoverers have a small menu of rehearsed moves, each of which re-enters the loop at a known step:

  • Say what you know. Out loud, list the true facts: input shape, constraints, what the brute force costs. Narrating known truths restarts the machine and emits signal while it does.
  • Shrink n. Can't solve it for n? Solve it for 3. The tiny case's solution usually contains the general one, embryo included.
  • Try sorting. "Would this be easier sorted?" unlocks two pointers, binary search, greedy scans — half of Part II. If order doesn't matter to the answer, sorting is a free move.
  • Try a hash map. "What is my inner loop re-finding, and could I have remembered it?" — the single highest-yield unstick question in the catalog.
  • Draw it. Pointers, trees, windows — half of these patterns were discovered by someone doodling boxes and arrows.

Pick your flavor of stuck below and get the move plus the literal line to say while making it.

Interactive · stuck-o-meter Pick your flavor of stuck; get the unstick move and the exact line to say
unstick move
rejoin the loop at
stuck states explored
0 / 6
💡
Every unstick move is a re-entry point into the loop, not a new strategy. Confused → step 1. No approach → step 3. Too slow → step 4. Buggy → step 6. That's the deeper reason to drill the framework: when you're panicking, "which step am I on?" is an answerable question, and "what do I do now?" follows from it mechanically.

8Scripts — what to literally say

Narration under pressure fails unless it's pre-written. These aren't suggestions of tone; they're lines to memorize verbatim, one per step, so your mouth has a default while your head works:

  • Opening (step 1): "Let me make sure I understand: we're given …, and we return …. A couple of quick questions before I start —"
  • Example (step 2): "Let me work a small case by hand to make sure I've got the behavior right."
  • Brute force (step 3): "The straightforward approach is X, which is O(n²). Let me see if there's structure to do better."
  • Pattern (step 4): "The expensive part is the re-scanning — if I remember what I've seen in a hash map, this drops to O(n). This is a counting/lookup problem."
  • Coding (step 5): "I'll write the skeleton first and narrate as I go — guard, count up, count down, verdict."
  • Testing (step 6): "Before I call this done, let me trace it on my example… and check the empty and mismatched-length cases."
  • Closing: "That's O(n) time and O(1) space for a fixed alphabet. Happy to talk follow-ups — a streaming version would change X."

Say them out loud during practice — every rep of every problem, until the lines are muscle. In the room, the loop plus the scripts means there is no moment where you're deciding what kind of thing to say next. That decision was made weeks ago; Chapter 24 schedules the reps.

The pattern, as a whiteboard skeleton:

  1. 1Restate the problem in your own words; get the nod before anything else.
  2. 2Clarify what changes the solution: indices or values, sorted?, duplicates?, empty input?, and the size of n (your complexity budget — ch. 3).
  3. 3Work one small example by hand — a vanilla case plus one built to break your first guess.
  4. 4State the brute force with its price: "naive is X, costs O(…)". That's your floor.
  5. 5Hunt structure, name the pattern: what's re-computed? sorted? counted? nested? Say the pattern's name out loud.
  6. 6Code the memorized skeleton first, narrating; fill in problem-specific details second.
  7. 7Trace your example through the real code, then the edge cases from step 2.
  8. 8Close with the complexity and offer a follow-up thought. If stuck at any point: say what you know, shrink n, try sorting, try a hash map.

9Valid Anagram, six steps later — the loop leaves artifacts

Here's the framework's output for Valid Anagram Easy, with the steps visible in the code itself. The brute force from step 3 — sort both, compare, O(n log n) ("sort once, then walk") — ships first, because it's the floor and it's honest. Then the step-4 insight: the restatement said counts, so a counting hash map does it in O(n) — count up over s, count down over t, and any dip below zero convicts. The length guard at the top is the step-1 clarification, cashed in as one line.

Read the comments as narration — they're roughly the sentences you'd be saying while typing. This counting map is your first real taste of Chapter 4, where it graduates from trick to pattern.

🎯
The tell: "rearrangement / anagram / same characters" — anything whose restatement contains the word counts — is a frequency-map problem. Sorting also works and is worth saying (it's the brute force), but the counting map beats it on both time and interview signal.