The edge cases to try every time
After this: Try five kinds of edge case, and find the bug in a function that starts from 0 and in a Java sum that is too large.
On this page · 7 sections · 3 checks
Your code gives the right answer on the example in the problem. You say "done", and the interviewer asks: "What happens with an empty list?" You had not thought about it, and now you find out at the worst moment. This lesson is about asking that question yourself, every time, before anyone else does.
Five inputs to try every time
An is an input at the limit of what the problem allows. Most bugs live there, because the example in the problem is almost never one. Try these five on every problem:
- Empty: no items at all.
- One item: the smallest input that is not empty.
- Duplicates: the same value more than once.
- Negative numbers, and zero.
- Very large: a long input, or very large values.
A bug that the example hides
Here is a function that should return the largest number in a list. It starts with best = 0int best = 0; and keeps any item larger than best. Try it on the five kinds of input:
| Input | Returns | Right answer |
|---|---|---|
| [4, 9, 2] | 9 | 9 |
| [-3, -1, -2] | 0 | -1 |
| [] | 0 | no answer |
On [4, 9, 2] it looks right. On [-3, -1, -2] no item is larger than 0, so best never changes and the function returns 0, a number that is not even in the list. On the empty list it returns 0 as well, when there is no largest number at all.
The fix starts from the first item, and handles the empty list before anything else by returning the missing marker, Nonenull. Now [-3, -1, -2] gives -1, [] gives Nonenull, [7] gives 7 and [5, 5, 5] gives 5.
Java does not run here: the Run button runs the Python version, and with Java selected you can read the Java code and run it on your computer. Run both functions on the five lists. Each line shows the list, the buggy answer and the fixed answer.
Very large has two meanings
A long input. An answer that tries every pair makes about half a million million checks on a million items, half of the 10¹² in lesson 2's table. If the problem allows a million items, say what that means for your solution's cost.
Large values. Python whole numbers grow as large as they need to. Java's int does not: it stops at 2,147,483,647. A sum larger than that does not give an error. It wraps round to a negative number, with no warning. This is called . Two billion plus two billion is -294,967,296 as an int and 4,000,000,000 as a long.
Overflow is why Java binary search writes lo + (hi - lo) / 2 for the middle. With lo at 1,500,000,000 and hi at 2,000,000,000, (lo + hi) / 2 adds first, overflows, and gives -397,483,648. The safe form never adds the two, and gives 1,750,000,000.
a = 2_000_000_000b = 2_000_000_000print(a + b)
lo = 1_500_000_000hi = 2_000_000_000print((lo + hi) // 2, lo + (hi - lo) // 2)4000000000 1750000000 1750000000
public class Main { public static void main(String[] args) { int a = 2_000_000_000; int b = 2_000_000_000; System.out.println(a + b); System.out.println((long) a + b);
int lo = 1_500_000_000; int hi = 2_000_000_000; System.out.println((lo + hi) / 2 + " " + (lo + (hi - lo) / 2)); }}-294967296 4000000000 -397483648 1750000000
Edges of a problem you know
The best day to buy and sell from the ten classic problems returns 0 when no profit is possible. Its edges:
| Prices | Profit | Why it is worth trying |
|---|---|---|
| [] | 0 | empty: no day to buy on |
| [5] | 0 | one day: you cannot sell on a later day |
| [3, 3, 3] | 0 | duplicates: no price rises |
| [2, 1] | 0 | prices only fall |
| [1, 2] | 1 | the smallest input with a profit |
def max_profit(prices): lowest = float("inf") best = 0 for p in prices: lowest = min(lowest, p) best = max(best, p - lowest) return best
for prices in [[], [5], [3, 3, 3], [2, 1], [1, 2]]: print(prices, max_profit(prices))[] 0 [5] 0 [3, 3, 3] 0 [2, 1] 0 [1, 2] 1
import java.util.*;
public class Main { static int maxProfit(int[] prices) { int lowest = Integer.MAX_VALUE; int best = 0; for (int p : prices) { lowest = Math.min(lowest, p); best = Math.max(best, p - lowest); } return best; }
public static void main(String[] args) { int[][] lists = { {}, {5}, {3, 3, 3}, {2, 1}, {1, 2} }; for (int[] prices : lists) { System.out.println(Arrays.toString(prices) + " " + maxProfit(prices)); } }}[] 0 [5] 0 [3, 3, 3] 0 [2, 1] 0 [1, 2] 1
For two sum, the edge case that matters most is duplicates: [3, 3] with target 6 is where checking before storing shows its worth, as you saw in Test the edges before you say done.
The rule
Before you say "done", go through the five kinds out loud: "Empty gives an empty answer. One item has no pair. Duplicates…". For each, say what your code returns and whether that is right. When one is wrong, you have found the bug yourself, which is worth far more than having it found for you.
Check yourself
The buggy function starts with best = 0int best = 0; and keeps any item larger than best. Which input shows that it is wrong?
Two whole numbers of two billion each are added. What does this print in PythonJava?
a = 2_000_000_000b = 2_000_000_000print(a + b)public class Main { public static void main(String[] args) { int a = 2_000_000_000; int b = 2_000_000_000; System.out.println(a + b); }}Which edge case matters most for the hash-map two sum, the one that checks for the partner first and stores the number after?
Honesty notes
- Try five kinds of edge case every time: empty, one item, duplicates, negative numbers and zero, very large.
- Find the bug in a largest function that starts from 0: it returns 0 for [-3, -1, -2], and the fix starts from the first item and handles the empty list.
- Say why Java's int overflows (2,000,000,000 + 2,000,000,000 is -294,967,296) while Python whole numbers do not, and why binary search writes lo + (hi - lo) / 2 in Java.
- Run the stock problem on its edges: empty, one day, equal prices and falling prices all give 0, and [1, 2] gives 1.