Practice / macOS

Debug a failing coding test

Trace one visible failure, make a small fix, and confirm it with the local runner.

Coding with the interview workflow, starter code, visible tests, and Coaching.
Coding with the interview workflow, starter code, visible tests, and Coaching. Select the image to enlarge.
  1. Read one failure closely

    Expand a failed visible case. Record the arguments, expected output, actual output, and the first behavior that looks wrong.

    Example

    In Two Sum, [3, 2, 4] and target 6 should return [1, 2]. Returning [0, 0] suggests your code paired the first value with itself.

  2. Isolate the cause

    Use the visible case’s debug control, Quick Test, or Sandbox where available. Trace state before and after the line you suspect.

    Example

    At index 0 the seen map should be empty. Inserting 3 before checking the complement allows the lookup to find index 0.

  3. Fix one behavior and rerun

    Move the lookup before insertion, or make the smallest change that restores the intended invariant. Rerun the failing input and then all visible cases.

    Example

    Try [3, 3] with target 6 as a custom case. You should return [0, 1] and still pass the previously working cases.

Check your work

You can identify the first incorrect state, show the fix with a test, and rerun the visible suite.

Try it yourself

Try a second bug in Two Sum. Use a truthiness check for an index retrieved from a map, then run the visible example [2, 7, 11, 15] with target 9.

What to look for
The value 2 is stored at index 0. A truthiness check can skip it. Use an explicit membership check and rerun the case to confirm [0, 1].