The Coding Interview
The coding interview is a structured problem-solving session disguised as a programming test. The interviewer watches how you move from an ambiguous prompt to working, tested code while explaining yourself. Most candidates have the knowledge; the ones who fail usually lose the structure.
The 60-second answer
Section titled “The 60-second answer”I use UMPIRE: Understand by restating the problem and asking about inputs, outputs, constraints and edge cases; Match it to a known pattern; Plan the approach and state its complexity before writing anything; Implement in small, named pieces; Review by tracing a small example; and Evaluate by testing edge cases and stating the final complexity. I talk throughout, because the interviewer scores my reasoning, not just the code. If I get stuck I fall back to brute force and optimise, or work a tiny example by hand.
UMPIRE, step by step
Section titled “UMPIRE, step by step”Understand
Section titled “Understand”Repeat the problem in your own words and force the ambiguities out:
- Input types and sizes? Sorted? Unique? Can it be empty?
- Expected output for an edge case — no match, duplicates, negatives?
- Is the input mutable? May I use extra memory? What does “best” mean — fastest, or least memory?
Write the constraints down. n ≤ 10^5 says O(n log n) is fine and O(n²) is not — that single
number often decides the approach.
Say which pattern the problem resembles: hash map for lookup, two pointers on a sorted array, sliding window for a contiguous constraint, BFS for shortest path in an unweighted graph, DFS/backtracking for enumeration, DP for overlapping subproblems, a heap for “top k”, binary search on the answer for monotonic feasibility.
Matching is a hypothesis, not a commitment: “this looks like a sliding window because the window is contiguous and we need a max over it” is exactly the reasoning interviewers want.
State the algorithm in plain English, then its complexity, then get a nod before coding.
Plan: one pass with a hash map from value -> index.For each element, check whether target - value is already in the map.Time O(n), space O(n).Implement
Section titled “Implement”- Small, well-named helpers rather than one 40-line block.
- Use the language’s idioms; you are being scored on fluency.
- Handle trivial cases first so the loop can assume valid input; narrate decisions, not syntax.
def two_sum(nums: list[int], target: int) -> list[int]: seen: dict[int, int] = {} for i, n in enumerate(nums): complement = target - n if complement in seen: return [seen[complement], i] seen[n] = i raise ValueError("no solution")Review
Section titled “Review”Trace the code by hand on the smallest interesting input. This catches off-by-one errors, inverted conditions and mutation bugs before the interviewer does — say the variable values as you go.
Evaluate
Section titled “Evaluate”State the final complexity with justification, then name the trade-off: “O(n) time, O(n) space; if the array were sorted I could do it in O(1) space with two pointers — probably your follow-up.”
Complexity: what to say
Section titled “Complexity: what to say”- Amortised — a hash map insert is O(1) amortised; a dynamic array append is O(1) amortised.
- Best/average/worst — quicksort is O(n log n) average and O(n²) worst; say which you mean.
- Space includes output and the recursion stack. Recursive DFS is O(h) stack space even with no explicit structures.
| Constraint | Reasonable target |
|---|---|
| n ≤ 20 | Exponential, backtracking, bitmask DP |
| n ≤ 1,000 | O(n²) |
| n ≤ 10^5 – 10^6 | O(n log n) or O(n) |
| n ≥ 10^7 | O(n) with low constants, streaming |
Testing in the room
Section titled “Testing in the room”Test in this order, saying what each checks: empty or single element; the prompt’s example; duplicates or negatives; boundaries (first and last index, maximum size); a case that should return nothing. If you find a bug, fix it without apologising for ten seconds — “good catch, let me handle that”.
Getting unstuck
Section titled “Getting unstuck”Being stuck is normal and is itself assessed. Have a ladder:
- Solve a tiny case by hand. n = 3, or the smallest failing input. The pattern usually appears.
- Start with brute force. State its complexity, then ask what is being recomputed.
- Relax a constraint. What if the array were sorted? If all values were positive?
- Change the representation. Sort it, index it, build a graph, use a stack.
- Ask a narrow question. “Can I assume the input fits in memory?” beats “I’m stuck”.
Practice plan
Section titled “Practice plan”- Pattern-first. 8–10 problems per pattern before moving on: arrays, hash maps, two pointers, sliding window, stacks, binary search, linked lists, trees, graphs, heaps, backtracking, DP.
- Timed and out loud. 35 minutes, one problem, narrating to a recorder. Review the recording; it is uncomfortable and it works.
- One language. Fluency beats breadth; you cannot afford to fight syntax under observation.
Follow-up questions
Section titled “Follow-up questions”- What is the complexity, and how would it change if the input were sorted?
- What if the input does not fit in memory?
- What if the function is called concurrently?
Common traps
Section titled “Common traps”- Coding before clarifying. You solve the wrong problem quickly.
- Ignoring the constraints. O(n²) for n = 10^6 loses the round.
- Silent thinking. The interviewer cannot score what they cannot hear.
- Not testing. The most common reason a correct solution scores badly.
Further reading
Section titled “Further reading”- Tech Interview Handbook: coding interview
- NeetCode roadmap — pattern-ordered practice.