A 30-minute coding interview practice routine

ยท Interview Smasher

Use one problem and spend part of the practice explaining your reasoning out loud. The goal is to make your decisions easy for an interviewer to follow while you write and test code.

Structure a 30-minute session

  1. Minutes 0 to 5. Restate the input and output. Ask about empty input, duplicates, and whether a solution is guaranteed.
  2. Minutes 5 to 10. Explain a correct baseline and its complexity. Pick an example you can trace by hand.
  3. Minutes 10 to 24. Improve the baseline, write the solution, and explain one trade-off as you go.
  4. Minutes 24 to 30. Walk through a normal case and a boundary case. State the final time and space complexity.

Worked example with Two Sum

Given an array and a target, find the indices of two distinct elements that add to the target. A pair scan uses quadratic time. A hash map stores values and their indices as you go, so each new value can check whether its complement was seen earlier. Checking before inserting avoids using the same index twice. This uses expected linear time and linear additional space.

Try [2, 7, 11, 15] with target 9 for the basic case, then [3, 3] with target 6 to test duplicates. Before coding a no-solution case, ask what the function should return when there is no pair.

Python implementation and test cases

This version returns an empty list when no pair exists. Confirm that convention with the interviewer before you implement it.

def two_sum(nums, target):
    seen = {}
    for index, value in enumerate(nums):
        complement = target - value
        if complement in seen:
            return [seen[complement], index]
        seen[value] = index
    return []


assert two_sum([2, 7, 11, 15], 9) == [0, 1]
assert two_sum([3, 3], 6) == [0, 1]
assert two_sum([3, 4], 6) == []

Trace the duplicate test out loud. At index 0, the map is empty, so you store the first 3. At index 1, you find the complement in the map and return two different indices. If asked to return every pair, explain why storing a single index per value would lose earlier duplicates and how you would represent them instead.

Use feedback after your attempt

Write and test your answer first. Then use a peer review or a tool to ask for one missed edge case and one explanation you could make clearer. The Interview Smasher browser demo has a Screen OCR scenario with Two Sum and a sample explanation. Use outside assistance in an actual assessment only when its rules allow it.

Plan your internship practice or prepare your behavioral answers.