Skip to content

Lesson 4 of 32The interview

The Coding Interview Playbook

Run any 45-minute coding interview with the same eight steps, from clarifying the problem to stating complexity.

16 min read0/5 questions

A coding interview is not a test of whether you have seen the problem before. It is 45 minutes of an engineer watching how you think. Candidates who follow the same plan every time stay calm, show more of their reasoning, and score higher even when they do not finish. This lesson gives you that plan.

The 45-minute plan

Here is roughly how the time splits in a single-problem round. The first 5 minutes are small talk and the last 5 are your questions for them, so you have about 35 minutes of real work.

MinutesStepWhat you are doing
0–3Clarify and restateRepeat the problem in your words, ask about inputs and outputs
3–6Examples and edge casesWork one example by hand, list the awkward inputs
6–9Brute force out loudDescribe the simplest correct approach and its cost
9–14Pattern match and optimiseName the pattern, find the faster idea, agree on it
14–30Code while narratingWrite clean code, explaining each block
30–35Test by handTrace an example and an edge case through your code
35–37ComplexityState time and space, and why
Put it in order6 steps

Put the playbook steps in order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.

Steps 1–3: understand before you solve

Clarify and restate. Say the problem back: "So I get an array of integers and a target, and I return the indices of two different elements that add to the target." Then ask the questions that change the solution:

  • Can the input be empty? Can it contain negatives or duplicates?
  • Is it sorted? How large can it be (100 items or 10 million)?
  • Is there always exactly one answer? What do I return if there is none?
  • Should I optimise for time or memory?

Examples and edge cases. Write a small example and solve it by hand. This catches misunderstandings early, which is far cheaper than catching them at minute 30. Then list edge cases: empty input, one element, all duplicates, negatives, the answer at the very end.

Brute force out loud. Say the obvious solution even if it is slow: "The simplest approach checks every pair with two loops. That is O(n²) time and O(1) space." Now you have a correct fallback, and the interviewer knows you can reach something. Do not code it unless the interviewer asks.

Pythonbrute_force.py
def two_sum_brute(nums, target): for i in range(len(nums)): for j in range(i + 1, len(nums)): if nums[i] + nums[j] == target: return [i, j] return [] print(two_sum_brute([2, 7, 11, 15], 9))print(two_sum_brute([3, 2, 4], 6))
Output
Press Run (⌘ Enter) to see what happens.
Quick check
The interviewer says: 'Find two numbers in the list that sum to the target.' Which question changes the best solution the most?
Need a nudge? A hint rules out a wrong answer.

Steps 4–5: optimise, then code

Pattern match. Ask yourself what the brute force repeats. In two sum, the inner loop searches for target - x again and again. Searching is what a hash map makes O(1). That is the move: name the waste, then name the tool that removes it. The practice page groups problems by pattern so you can train this reflex: hashing, two pointers, sliding window, stack, binary search, trees, graphs, heaps, backtracking, DP, greedy, intervals.

Match the pairs0/5 matched

Match the clue in the problem to the pattern it usually points to.

Choose an item, then its match on the other side.

Agree before coding. Say: "I'll use a hash map from value to index. One pass, O(n) time and O(n) space. Does that sound good?" The interviewer will nod or steer you. Either way, you did not waste 15 minutes coding the wrong thing.

Code while narrating. Use clear names (seen, not d). Write small helper functions if logic repeats. Narrate at the level of intent ("now I check whether the complement is already in the map"), not syntax ("now I type a colon").

Pythonoptimal.py
def two_sum(nums: list[int], target: int) -> list[int]: seen: dict[int, int] = {} # value -> index for i, x in enumerate(nums): need = target - x if need in seen: return [seen[need], i] seen[x] = i return [] print(two_sum([2, 7, 11, 15], 9))print(two_sum([3, 3], 6))print(two_sum([1, 2], 10))
Output
Press Run (⌘ Enter) to see what happens.

This is the trace you would say out loud during testing, for nums = [2, 7, 11, 15] and target = 9, then for a duplicate case:

Dry run of the seen map
  1. 0empty
  2. 1empty
  3. 2empty
  4. 3empty
  5. 4empty
  6. 5empty
  7. 6empty
  8. 7empty

Start with an empty map. It will hold value -> index for everything already passed.

1/5
Predict the outputPython

What does this print?

seen = {}
nums = [4, 1, 5, 3]
target = 8
for i, x in enumerate(nums):
  if target - x in seen:
      print(seen[target - x], i)
      break
  seen[x] = i
Need a nudge? Hints walk you through it.

Steps 6–7: test and analyse

Test by hand. Do not say "I think it works." Pick your small example and trace it line by line, saying the variable values out loud. Then run one edge case (empty list, or no answer). Interviewers mark "found own bug" as a positive; "interviewer found the bug" is a negative.

Complexity. State time and space and why: "One pass over n elements with O(1) dictionary operations, so O(n) time. The dictionary can hold up to n entries, so O(n) space."

Quick check
You finish coding with 6 minutes left. What should you do first?
Need a nudge? A hint rules out a wrong answer.

When you are stuck, and how to take hints

Getting stuck is normal and expected. Silence is the problem, not being stuck. Try these, out loud:

  • Go back to the example. Solve it by hand slowly and notice what you did. That is often the algorithm.
  • Simplify. "What if the array were sorted?" "What if k were 1?" Solve the easy version, then generalise.
  • Run the pattern list. "Could a hash map help? Two pointers? A heap?"
  • Say what you know. "I know the brute force is O(n²), and the repeated work is the inner search."

Taking hints well. A hint is not a penalty; ignoring one is. When the interviewer says "what if you stored something as you go?", stop, repeat it back, and follow it: "Storing... so I could remember values I've already seen, and check the complement in O(1)." Arguing with a hint is one of the strongest negative signals.

A sample transcript (first 8 minutes)

Interviewer: Given an array of integers and a target, return the indices of the two numbers that add up to the target.

You: Let me restate: I get a list like [2, 7, 11, 15] and a target like 9, and I return [0, 1] because 2 plus 7 is 9. Can I use the same element twice? Is there always exactly one answer? Could the list be empty?

Interviewer: Not the same element. Assume one answer exists. It won't be empty.

You: And could there be negatives or duplicates? Is it sorted?

Interviewer: Both are possible. Not sorted.

You: OK. Brute force: check every pair, O(n²) time, O(1) space. The waste is the inner loop searching for target - x. If I keep a hash map of value to index as I go, each lookup is O(1), so one pass is O(n) time and O(n) space. Duplicates like [3, 3] with target 6 work because I check before I insert. Sound good?

Interviewer: Go for it.

Common mistakes

  • Coding in the first two minutes, before agreeing on the approach.
  • Long silences. If you need to think, say "Let me think for 30 seconds" and then share what you thought.
  • Optimising prematurely: aiming for the clever solution and finishing nothing. A working O(n²) beats an unfinished O(n).
  • Not testing, or only testing the happy path.
  • Getting defensive about a bug. Say "good catch" and fix it.

Practice the full eight steps on these two. Talk out loud even when you are alone; it feels strange for a week, then it becomes your normal way of solving.

ChallengeEasy

Two Sum

Given a list of integers nums and a number target, find the two different positions whose values add up to target. Return their indices as a list in increasing order.

nums = [2, 7, 11, 15], target = 9    →  [0, 1]   (2 + 7)
nums = [3, 2, 4],      target = 6    →  [1, 2]   (2 + 4)
nums = [3, 3],         target = 6    →  [0, 1]

You may not use the same index twice.

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • Exactly one valid answer exists.
def two_sum(nums: list[int], target: int) -> list[int]: # For each number, what partner would you need? pass
ChallengeEasy

Valid Parentheses

Given a string s made only of the characters ()[]{}, return true if every bracket is closed by the same type of bracket, in the right order. An opening bracket must be closed before any bracket opened earlier is closed.

"()[]{}"   →  true
"{[]}"     →  true
"([)]"     →  false   (] closes while ( is still the latest open)

Constraints

  • 0 <= s.length <= 10^4
  • s contains only the characters ()[]{}.
  • An empty string counts as valid.
def is_valid(s: str) -> bool: # Push opening brackets; each closing bracket must match the latest one. pass
Key terms
brute force
The simplest correct approach, usually trying every option. State it first as a fallback, with its complexity.
edge case
An unusual input (empty, one element, duplicates, negatives) that often breaks a naive solution.
pattern matching
Recognising which known technique (hash map, two pointers, heap, DP) fits a problem's clues.
narrating
Explaining your intent out loud while coding so the interviewer can record your reasoning.
dry run
Tracing an example through your code by hand, tracking variable values, to find bugs before running it.