In this lesson, we'll walk through a practical checklist of edge cases you should test your code against. We'll cover edge cases related to input size, values, and data structures, as well as cases that arise from the answer itself.
We'll also learn how to use this checklist effectively during a coding interview without spending too much time on testing.
An edge case is an input that falls at the boundary of what the problem allows or has an unusual structure. An empty array, an array with a single element, or an array where every element is identical are all examples.
Your code might work perfectly on the given examples and still fail on these inputs. This usually happens because your code makes an assumption, such as the array having at least two elements or a valid answer always existing.
Edge cases help expose these hidden assumptions.
The challenge is that under interview pressure, it's difficult to remember every case worth checking. That's where a checklist comes in handy. Instead of relying on memory, you go through a small set of categories and identify which ones apply to your problem.
The checklist has five categories:
You can use this checklist at two points:
Let's go through each category.
Start with an empty input. Can the array, string, or list be empty? If so, think about how your code handles it.
For example, if you're finding the maximum of an array and initialize it with the first element, your code will crash when the array is empty.
Next, consider a single element. Many algorithms compare neighboring elements or use two pointers, but with just one element, there's nothing to compare. Make sure your loops don't accidentally skip that element or access an index outside the array.
Then, check an input with two elements. It's a great way to see whether your pointers move, meet, and cross correctly.
In binary search, for example, the difference between using left < right and left <= right becomes much easier to spot on small inputs.
If your loop condition is incorrect, a one-element or two-element array will often expose the bug.
The second category involves the values in the input.
Let's start with negative numbers. Consider the problem of finding the maximum sum of a contiguous subarray.
If you initialize the best sum to zero, an array containing -3, -1, and -2 would give you 0. But the correct answer is -1, because the subarray must contain at least one element.
Assuming the input is non-empty, initializing the best sum to the first element avoids this issue.
Next, consider zero. Zero can cause division-by-zero errors and change the behavior of calculations involving multiplication.
For example, when calculating the product of all elements except the current one:
Your solution must handle both cases correctly.
Next, consider duplicates. If you're looking for two numbers that add up to 6 in an array containing 3 and 3, your solution must treat the two threes as separate elements.
Similarly, if your binary search is supposed to return the first occurrence of a value, testing with duplicates helps verify that it returns the correct index.
Finally, test an input where every element is identical, as this can expose several edge cases at once.
There are two types of extreme inputs to consider.
The first is the largest input size allowed by the constraints. Your solution should remain efficient even at this size, so make sure its time complexity fits within the given constraints.
Recursive solutions require special attention here. If each recursive call adds another level to the call stack, a sufficiently large input can cause a stack overflow.
The second type involves extremely large or small values, which can cause integer overflow.
In languages like Java and C++, a signed 32-bit integer can hold values up to roughly 2.1 billion (2,147,483,647). If you add together 100,000 numbers, each as large as one billion, the sum can reach 100 trillion, far beyond what a 32-bit integer can store. In such cases, use a 64-bit integer type, such as long in Java or long long in C++.
Another classic example is calculating the middle index in binary search as (left + right) / 2. Even if both indices individually fit within the integer range, adding them together can cause an overflow.
A safer approach is to calculate mid as left plus half the difference between right and left:
This gives us the same result without risking overflow when adding the two indices.
Python integers support arbitrary precision, so integer overflow isn't a concern when using regular Python integers. However, it's something you need to watch out for in Java, C++, and other languages with fixed-width integer types.
The fourth category depends on the data structure you're working with.
Check an empty string and a string with a single character. Also consider how the problem treats uppercase and lowercase letters, spaces, and punctuation. For example, a palindrome check might require you to ignore everything except letters and digits.
Check an empty list, a single node, and operations that modify the head or tail.
Removing the head often requires special handling because there's no previous node whose pointer you can update. Adding a dummy node before the head allows you to handle this using the same logic as any other node.
Check an empty tree, a single node, and a skewed tree, where each non-leaf node has only one child. A completely skewed tree behaves like a linked list, so its height grows linearly with the number of nodes.
Check whether the graph can be disconnected. If you need to visit every node, you'll have to start a search from each unvisited node. Also consider cycles and self-loops, which can cause repeated visits without proper tracking.
Check an empty matrix, a single row, a single column, and a non-square matrix.
The fifth category comes from the answer rather than the input.
-1, an empty list, or false.During an interview, you won't have time to test every case on this checklist, and you don't need to.
Before you start coding, clarify the edge cases that could affect your approach:
Once you've finished coding, pick two or three cases that are most likely to break your specific solution and walk through your code with each one:
Whenever possible, handle edge cases naturally through your main logic rather than adding separate checks for each one. Initializing a maximum with the first element, introducing a dummy node, or choosing the right loop condition can often handle edge cases without adding unnecessary complexity.
If your existing logic already handles a particular edge case, mention it out loud so the interviewer knows you've considered it.
Most edge cases fall into five categories: