Test the edges before you say done
After this: Test the answer on six edge cases and against the brute force.
On this page · 4 sections · 3 checks
Your code works on [3, 2, 4]. Is it done? Not yet. One example that works shows very little, because many mistakes only show on unusual inputs. Step 6 is to try the inputs most likely to break the code, before the interviewer does.
Where code breaks
Code usually breaks at the edges, the inputs at the limit of what the problem allows. Lesson 1's questions already named some of them: repeated numbers, negative numbers, and no answer at all. Add the smallest lists, one item and none, and you have a short list to try every time.
| Input | Expected | Why it is worth testing |
|---|---|---|
[3, 3], 6 | [0, 1] | repeated numbers; storing first would give [0, 0] |
[-1, -2, -3, -4, -5], −8 | [2, 4] | negative numbers |
[0, 4, 3, 0], 0 | [0, 3] | zero, and the pair is the first and last item |
[1, 2], 7 | [] | no answer |
[5], 5 | [] | one item: no pair exists |
[], 0 | [] | empty |
Work the first row by hand with the code from lesson 4:
- Position 0 holds 3.
needis 3, andseenis empty, so store 3 → 0. - Position 1 holds 3.
needis 3, which is inseenat 0. Return[0, 1].
Two different items with the same value, and the answer is right. This row catches the store-first mistake from lesson 3, which returns [0, 0] here.
Test against the slow answer
You worked out the "Expected" column by hand, and hands make mistakes too. The brute force from lesson 2 gives you a second opinion: it tries every pair, so it is hard to get wrong. Run both functions on every edge case and compare. When they agree with each other and with the table, you can say "done" with good reason.
Each line shows the list, the target, the fast answer and the slow answer. All six agree with each other and with the table. Now try the store-first mistake: move the store line, seen[x] = iseen.put(x, i);, up so that it comes before the line that works out need, and run again. The first three lines of output change, and on each of them the fast answer no longer matches the slow one.
Check yourself
One item, target 5. What does this print?
def two_sum(nums, target): seen = {} # value -> position for i, x in enumerate(nums): need = target - x if need in seen: return [seen[need], i] seen[x] = i return []
print(two_sum([5], 5))import java.util.Arrays;import java.util.HashMap;
public class Main { static int[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> seen = new HashMap<>(); // value -> position for (int i = 0; i < nums.length; i++) { int x = nums[i]; int need = target - x; if (seen.containsKey(need)) { return new int[] {seen.get(need), i}; } seen.put(x, i); } return new int[0]; }
public static void main(String[] args) { System.out.println(Arrays.toString(twoSum(new int[] {5}, 5))); }}Lesson 3 warned about storing each number before checking. Which one of these tests would catch that mistake, by giving a wrong answer?
Why run the slow answer next to the fast one on your tests?
Honesty notes
- List the edge cases worth trying every time: repeated numbers, negative numbers, zero, no answer, one item, an empty list.
- Run the fast answer next to the brute force on every edge case and compare.
- Say which test catches the store-first mistake: a pair of equal numbers, such as [3, 3] with target 6.
- Remember that when two pairs add up, the two answers can return different pairs that are both right, so check that the pair adds up rather than comparing pairs.