CS concepts
Edge Cases Explained: 7 to Test Before You Submit Code
Updated Nov 27, 2026 · 3 min read
Before you say done, test seven edge cases: empty input, one element, all duplicates, negatives and zero, sorted and reverse sorted input, the answer at the first or last index, and the biggest n. Each one catches a common bug, from nums[0] crashing on [] to an O(n²) loop timing out at n = 100,000. Run 2 or 3 by hand and say them out loud.

Key points
- ●Empty input catches nums[0] crashing; one element catches nums[i + 1] running off the end.
- ●All duplicates catch first versus last bugs; negatives catch a max that starts at 0.
- ●Sorted and reverse sorted input are worst cases for many greedy and sorting ideas.
- ●Off-by-one bugs live at index 0 and len(nums) - 1.
- ●At n = 100,000, O(n²) is 10 billion steps, and Python's recursion stops near 1,000 deep.

Why edge cases decide interviews
Most interview answers work on the example in the problem. The ones that fail break on inputs nobody tried: an empty list, a single item, a row of negatives. Testing a few edge cases before you say done catches those bugs while you can still fix them, and it shows the interviewer you think about inputs, not just the happy path.
The visual sheet above groups seven cases into four families: input size, value patterns, ordering and answer position. Here's each one, with the bug it catches.
The seven edge cases
| # | Test | Example | Bug it catches |
|---|---|---|---|
| 1 | Empty input | [] or "" |
nums[0] crashes |
| 2 | One element | [5] |
nums[i + 1] runs off the end; two pointers start together |
| 3 | All duplicates | [2, 2, 2, 2] |
First versus last position; a set shrinks the input to one item |
| 4 | Negatives and zero | [-3, 0, -1] |
A max that starts at 0; two negatives multiply to a positive |
| 5 | Sorted and reverse sorted | [1, 2, 3, 4], [4, 3, 2, 1] |
Worst cases for many greedy and sorting ideas |
| 6 | Answer at the edges | index 0 or len(nums) - 1 |
Off-by-one errors |
| 7 | The biggest n | n = 100,000 | O(n²) timeout; recursion depth |
What each one is really checking
1. Empty input. Any line that reads nums[0] before checking the length will crash. Decide what an empty input should return, usually 0, False or [], and handle it first.
2. One element. Loops that compare nums[i] with nums[i + 1] read past the end when there's only one item. Two pointers that start at both ends begin on the same spot, so check that your loop condition still does the right thing.
3. All duplicates. If the question asks for the first or last position of a value, [2, 2, 2, 2] shows whether you return the right one. It also exposes code that turns the input into a set and quietly loses items.
4. Negatives and zero. A running maximum that starts at 0 is wrong when every number is negative. Products flip too: two negatives multiply to a positive, and a zero wipes everything out.
5. Sorted and reverse sorted. These are the classic worst cases. A greedy choice that looks fine on random input can fall apart when everything arrives in order, or in reverse.
6. Answer at the edges. Put the target at index 0 and at the last index. Off-by-one bugs hide exactly there, in range bounds and in < versus <=.
7. The biggest n. Read the constraints. At n = 100,000, an O(n²) loop does 10 billion steps and times out. And Python's default recursion limit is about 1,000, so a recursive solution that goes one level per element can crash long before it's slow.
How to use the list in an interview
You don't need all seven every time. Pick the two or three that fit the problem:
- Array or string input: start with empty and one element.
- Anything with a max, min or product: add negatives and zero.
- Search for a position: add all duplicates and the answer at the edges.
- Nested loops or recursion: check the biggest n against the constraints.
Then run them by hand, out loud. "If the list is empty, the loop never runs and we return 0, which is right." That one sentence tells the interviewer more than a silent pass.
Quick recall
The sheet compresses it into one line per family:
- Size: empty, then one, then the biggest n.
- Values: duplicates, then negatives and zero.
- Order: sorted, then reverse sorted.
- Position: first item, then last item.
- Action: run 2 or 3 by hand and say them out loud.
Learn the families, not the list. When a new problem shows up, walk the five lines and the right tests fall out.
Frequently asked questions
What edge cases should I test in a coding interview?
Empty input, one element, all duplicates, negatives and zero, sorted and reverse sorted input, the answer at the first or last index, and the largest input size. Together they cover input size, value patterns, ordering, answer position and complexity.
Why does a maximum that starts at 0 fail?
If every number is negative, like [-3, -1], no value ever beats 0, so the code returns 0, which isn't in the input. Start the maximum at the first element or at negative infinity instead.
What is Python's recursion limit?
Python's default recursion limit is about 1,000 calls deep. A recursive solution that goes one level per element can crash on a large input even when its logic is right.
How many edge cases should I run by hand?
Two or three, picked for the problem in front of you, and say each one out loud as you check it. Interviewers listen for that habit.