Dry runs, edge cases and debugging by hand
1 What a dry run is
A dry run means running the code by hand. We pick a small input, go through the code one line at a time, and write down every variable after every step. The computer is not involved at all.
It sounds slow, but it is the fastest way to check an idea. A dry run on three or four values takes a minute, and it shows exactly where the steps go wrong. Interviewers also expect it: after writing code, a good candidate walks through it on an example without being asked.
We write the values in a trace table: one column per variable and one row per step.
2 A dry run that catches a bug
Here is a first try at "return the largest value in an array":
largest = 0
for each value x in nums
if x > largest
largest = x
return largest
A dry run on nums = [3, 8, 5]:
| Step | x | x > largest? | largest |
|---|---|---|---|
| Start | — | — | 0 |
| 1 | 3 | Yes | 3 |
| 2 | 8 | Yes | 8 |
| 3 | 5 | No | 8 |
It returns 8, which is right. One example passing proves very little, though. Now we try nums = [-4, -2, -7]:
| Step | x | x > largest? | largest |
|---|---|---|---|
| Start | — | — | 0 |
| 1 | −4 | No | 0 |
| 2 | −2 | No | 0 |
| 3 | −7 | No | 0 |
It returns 0, but 0 is not even in the array. The answer should be −2. The trace table shows the cause on its first row: largest starts at 0, which is bigger than every value here, so no value ever replaces it.
The fix is to start from a value that is really in the array, the first one:
A dry run of the fixed code on [-4, -2, -7] starts with largest = −4, replaces it with −2, and keeps −2. That is the right answer.
3 Edge cases: the inputs that break code
The bug above only showed up for one kind of input. Inputs like that are called edge cases: the smallest, largest or most unusual inputs the problem allows. Most Wrong Answers come from one of them.
The constraints tell us which edge cases exist. Before coding, we go down this list and ask which ones the constraints allow:
- The smallest input. One element,
n = 1, or an empty string, if the constraints allow it. - All values the same.
[5, 5, 5]often breaks code that expects a change somewhere. - Negative numbers and zero. These are the cases our first try at the largest value missed.
- The largest values. Values near 10⁹ can overflow a 32-bit
intwhen we add or multiply them. - Already sorted, or sorted backwards. These are worst cases for many methods.
- Duplicates. Two equal values can confuse code that assumes all values differ.
We do not need all six every time. A problem with 1 ≤ n has no empty input, and a problem with only positive values has no negatives. The point is to check the list on purpose rather than hope.
4 Debugging a Wrong Answer
When code gets a Wrong Answer, guessing and resubmitting wastes time. A steady method works better:
- Find a small input that fails. Use the edge-case list. A failing input with three values is far easier to trace than one with a thousand.
- Work out the right answer by hand for that input, before looking at what the code returns.
- Dry run the code on it, writing a trace table.
- Find the first row where the table goes wrong. The bug is in the line that produced that row.
When a dry run gets long, printing helps. We print the variables inside the loop, run the code on the small input, and read the printed values as a trace table the computer wrote for us. We remove the prints before submitting.
Explaining the code out loud, line by line, also works surprisingly often. People call this rubber-duck debugging, because the listener can even be a rubber duck. Saying each line in words forces us to notice what it really does, not what we meant it to do.
5 What each verdict asks us to check
Each verdict points to a different first check:
| Verdict | First thing to check |
|---|---|
| Compile Error | The first error message and its line number. Later messages are often caused by the first one. |
| Runtime Error | Every array index: can it go below 0 or past the end? Also any division where the bottom can be 0. |
| Wrong Answer | The edge-case list, then a dry run on the smallest failing input. |
| Time Limit Exceeded (TLE) | How much work the code does at the largest input. The Big-O lessons cover this. |
The habit to keep: after writing code, dry run it on one normal input and one edge case before submitting. It takes two minutes and saves most Wrong Answers.
Finished this lesson?
Mark it read and it is ticked on the roadmap and in the list.