Testing your solution before they do
The edge cases that break most submissions, and how to walk your own code convincingly.
Finding your own bug scores far better than having it found for you. Reserve the last five minutes and be systematic.
The standard edge cases
| Category | Cases |
|---|---|
| Size | Empty, one element, two elements |
| Values | All identical, all negative, zeros, the maximum value |
| Order | Already sorted, reverse sorted, one element out of place |
| Result | No valid answer, several equally valid answers |
| Structure | Single-node tree, degenerate tree, disconnected graph, self-loop |
| Strings | Empty string, single character, whitespace, non-ASCII |
The bugs that recur
Off-by-one. < versus <=, n versus n - 1. Check the last iteration explicitly.
Integer division and rounding. (lo + hi) / 2 without flooring; negative numbers rounding toward zero rather than down.
Mutating while iterating. Removing from a collection inside its own loop skips elements.
Shared references. new Array(3).fill([]) gives three references to one array. Use Array.from({ length: 3 }, () => []).
Uninitialised accumulators. let best = 0 breaks when every value is negative; start from -Infinity or from the first element.
Forgetting to return. Especially inside recursion — computing the answer and dropping it.
Walk the code, do not skim it
Pick your worked example and trace it line by line, saying variable values aloud. Actually track them:
items = [3, 1, 2], target = 3
i=0: seen={}, need=0, not found, seen={3:0}
i=1: need=2, not found, seen={3:0, 1:1}
i=2: need=1, found at index 1 → return [1, 2] ✓
Skimming re-reads your intention; tracing reads what the code says. They differ exactly where the bug is.
Say your invariant
"After each iteration, seen holds every value before i, and best is the answer for the prefix up to i." If the invariant holds at the start, is preserved by the loop body, and gives the answer at the end, the loop is correct. That is a proof, and it takes one sentence.
If you find a bug
Say what it is, why it happens, and fix it calmly. Finding and fixing a bug in front of the interviewer is a positive signal — it is exactly what the job involves.