Skip to content

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.

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.

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).
  • 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.
two-sum, one pass
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")

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.

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

  • 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

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

Being stuck is normal and is itself assessed. Have a ladder:

  1. Solve a tiny case by hand. n = 3, or the smallest failing input. The pattern usually appears.
  2. Start with brute force. State its complexity, then ask what is being recomputed.
  3. Relax a constraint. What if the array were sorted? If all values were positive?
  4. Change the representation. Sort it, index it, build a graph, use a stack.
  5. Ask a narrow question. “Can I assume the input fits in memory?” beats “I’m stuck”.
  • 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.
  1. What is the complexity, and how would it change if the input were sorted?
  2. What if the input does not fit in memory?
  3. What if the function is called concurrently?
  • 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.