Guides Guide 3 Process

The Interview Script

The same seven phases, every time, in the same order. A script frees your attention for the actual problem, and it keeps you moving when the problem is hard.

Two candidates can produce the same final code and get opposite verdicts. The difference is almost always the first ten minutes and the last five. This page is what to do in those minutes.

Contents

  1. The time budget
  2. 1. Clarify
  3. 2. Work an example by hand
  4. 3. State the brute force
  5. 4. Optimise, out loud
  6. 5. Code
  7. 6. Test
  8. 7. Complexity and follow-ups
  9. When you are stuck
  10. When the interviewer hints
  11. A worked transcript

The time budget

For a 45-minute slot with one problem. Adjust the middle if there are two.

PhaseMinutesOutput
1. Clarify3You can restate the problem in one sentence.
2. Example by hand2One worked case on the board, plus one edge case.
3. Brute force2Named and costed. Not coded.
4. Optimise6A pattern named, an approach agreed.
5. Code18Working code, narrated as you go.
6. Test6A dry run and the edge cases.
7. Complexity and follow-ups3Time, space, and one improvement you would make.
Do not start coding before minute 10. The most common failure is not a wrong algorithm, it is coding the right algorithm for the wrong problem. Ten minutes of alignment is cheap. Rewriting at minute 35 is not.

1. Clarify

Ask until you could write the function signature with no guesswork. Six questions cover most problems.

AskWhy it changes your answer
“How large can the input get?”It picks the target complexity. See Guide 2.
“Can the values be negative? Zero?”Kills or enables the sliding window outright.
“Can the input be empty, or have one element?”Decides your guard clauses, and shows you think about edges early.
“Are duplicates possible?”Changes dedup logic in 3Sum, backtracking, and rotated binary search.
“Am I allowed to modify the input?”Decides between cyclic sort and Floyd on the same problem.
“If several answers are valid, does it matter which?”Decides whether you need a tie-break, as in topological sort.
Close the phase by restating. “So: given an array that may contain negatives, count the contiguous subarrays summing to exactly k, with n up to a hundred thousand. Is that right?” If they say yes, you are aligned. If they correct you, you just saved twenty minutes.

2. Work an example by hand

Take the given example and actually compute the answer yourself, on the board, slowly. Two reasons: it forces you to understand the rule, and it frequently reveals the pattern before you have looked for it.

Do not invent five edge cases here. One is enough to show the habit. Five eats your coding time and reads as stalling. Save the rest for the testing phase, where they belong.

3. State the brute force

Say it. Cost it. Do not write it.

The template sentence: “The brute force is to check every pair of indices, compute the sum of each range, and count the matches. That is O(n²) time and O(1) space. With n at a hundred thousand that is 10¹⁰ operations, so it will not pass. Let me find something better.”

This costs thirty seconds and does three things. It proves you understand the problem. It gives you a correctness baseline for later. And it sets up the optimisation as a deliberate step rather than a lucky guess. Some interviewers will also let you keep it as a fallback if you run out of time, which is far better than nothing.

The one exception. If the brute force is the expected answer, given the constraints, say so and code it. “n is at most 20, so 2ⁿ is about a million and exhaustive search is intended.” Optimising past the requirement wastes time and can introduce bugs.

4. Optimise, out loud

This is the phase being graded hardest. Make your reasoning audible.

MoveWhat to say
Name the waste“The brute force recomputes the same range sums over and over.”
Name the pattern“Contiguous plus an exact target plus negatives, so a window will not work. This is prefix sums with a hash map.”
Justify it“A range sum is a difference of two prefixes, so I can look up the partner instead of scanning for it.”
Cost it before coding“That is one pass, O(n) time and O(n) space. That fits the constraint.”
Get agreement“Shall I code that?”
Always ask before coding. It takes two seconds and gives the interviewer a natural moment to redirect you. They usually want to. A candidate who checks in gets steered; a candidate who charges ahead gets watched failing.

5. Code

Write it the way you would want to read it in a review.

DoWhy
Start with the signature and typesIt confirms the contract one last time.
Guard clauses firstEmpty input, single element. Gets the edges out of the way.
Name variables for what they meanwindow_sum, not s. first_index, not d.
Write the invariant as a commentOne line. It is how you and the reviewer both stay convinced.
Narrate the non-obvious lines only“I look up before recording, so a prefix cannot pair with itself.”
Extract a helper when a block gets longSmall named functions read better under time pressure, not worse.
Do notWhy
Narrate every line“Now I set i to zero” is noise. Explain decisions, not syntax.
Go silent for three minutesThe interviewer cannot score thinking they cannot hear.
Golf itA dense one-liner is harder to debug and harder to grade.
Leave a TODO and move onFinish the piece you are on, or say clearly that you are deferring it.
Silently change approach mid-waySay “I have realised this does not handle X, I am switching to Y”.
Talking and coding at once is hard, and it is a learnable skill. Practise it deliberately: solve problems out loud, alone, on a timer. It feels ridiculous. It is the single highest-return thing you can rehearse.

6. Test

Do not say “I think that works”. Prove it, out loud, on the example still on the board. The method is in Guide 4. The short version:

  1. Dry-run the main example, tracking every variable in a small table.
  2. Run the edge case you invented in phase 2.
  3. Check the boundaries: first iteration, last iteration, and the exit condition.
  4. If you find a bug, say what it is before you fix it. Diagnosing out loud scores. Silently editing does not.
Finding your own bug is a positive signal, not a negative one. The interviewer is deciding whether they would trust your code without checking it. A candidate who catches their own off-by-one is more reassuring than one whose code happened to be right.

7. Complexity and follow-ups

State time and space using the four-part form in Guide 2. Then offer one thing you would do next. Good closers:

One is plenty. Volunteering an improvement you did not have time to build still demonstrates you saw it.

When you are stuck

Being stuck is normal and is not itself a failure. Being stuck and silent is. Work down this list, out loud.

MoveThe prompt to give yourself
Shrink itSolve n = 1, then n = 2, then n = 3 by hand. The recurrence often appears.
Re-read the constraints“n is at most 20” is an instruction. See the hint table.
Walk the decision tableSorted? Contiguous? All paths? Fewest steps? Run down the sixteen triggers.
Change the data structureWhat if this were sorted? In a hash map? On a heap? In a stack?
Invert the question“Fewest to remove” is “most to keep”. “Rooms needed” is “peak overlap”.
Solve a relaxed versionDrop a constraint, solve that, then add the constraint back.
Say where you are“I have the O(n²) version. I am looking for a way to avoid rescanning the left side. Can I think for a minute?”
Do not go silent, and do not apologise repeatedly. “Sorry, I am so slow” costs you twice: it wastes time and it invites the interviewer to agree. Replace it with a status update. “Here is what I have and here is what I am missing” is the same information, framed as progress.

When the interviewer hints

A hint is not a penalty. Interviewers hint because they want you to finish. How you take it is what gets scored.

They sayThey meanDo this
“What if the array were sorted?”Sort it.Take it immediately. “Good idea, then two pointers work because…”
“Do you need to recompute that?”You have a redundant scan.Find the repeated work. It is usually a cache or a running total.
“Walk me through your example again.”There is a bug and they want you to find it.Dry-run slowly. Do not defend the code.
“Is that the best you can do?”No.Say your current complexity, then name the next target.
“What happens if the input is empty?”Your code crashes.Trace it honestly, then add the guard.
Silence after you finishThey are waiting for the complexity.Give time, space and one follow-up.

A worked transcript

Problem: count the subarrays summing to exactly k. Compressed, but this is the shape.

Clarify. “How big can the array get? … A hundred thousand, so I need about O(n log n) or better. Can the values be negative? … Yes, good, that matters. Can the array be empty? … Yes, and then the answer is zero. And I want the count of subarrays, not one example?”
Example. “Let me take [1, 2, 3] with k = 3. Subarrays are [1], [1,2], [1,2,3], [2], [2,3], [3]. Two of them sum to 3, so the answer is 2. And an edge case: [0, 0] with k = 0 should be 3, because both singles and the pair all work.”
Brute force. “Every start, every end, sum each range. O(n²) with a running total, O(n³) if I re-sum naively. At a hundred thousand that is 10¹⁰, too slow. Let me improve it.”
Optimise. “A window is out, because values can be negative and the target is exact equality, so shrinking is not safe. But the sum of a range is a difference of two prefix sums. I want P[j] - P[i] = k, which rearranges to P[i] = P[j] - k. So as I walk along keeping a running prefix, I ask a hash map how many earlier prefixes equal running - k. That is the Two Sum rearrangement. One pass, O(n) time and O(n) space. Shall I code it?”
Code. “I will seed the map with zero mapped to one, standing for the empty prefix, otherwise I lose every subarray starting at index 0. And I look up before recording the current prefix, so a prefix cannot pair with itself and give me an empty subarray.”
Test. “On [1, 2, 3] with k = 3: running goes 1, 3, 6. At running = 3 I look for 0, which is in the map once, so count is 1. At running = 6 I look for 3, which is there once, so count is 2. Correct. On [0, 0] with k = 0: running stays 0. First step looks up 0 and finds the seed, count 1, then records, so the map has 0 twice. Second step looks up 0 and finds two, count 3. Correct.”
Close. “O(n) time, O(n) space for the map. n is the array length. The space is worst case when every prefix is distinct. If the question changed to the longest such subarray, I would store the earliest index per prefix instead of a count, and I would not overwrite an existing key.”

The five things to carry forward


Guide 2 — Constraints and Complexity Guide 4 — Testing Your Own Code