Dry-run with a table
After this: Run code by hand on a small input, one row per pass, and find a bug in a sliding window that way.
On this page · 5 sections · 3 checks
You have written your code and it looks right. In an interview there is often no way to run it, and "it looks right" is not a test. What you can do is run it yourself, on paper, with a small input, while the interviewer watches. This lesson shows how, and how it finds a real bug in four rows.
Running code by hand
A is running your code by hand on a small input. You draw a table with one column for each variable that changes and one row for each pass of the loop. Then you follow the code line by line and fill in each row. It is what the trace band does for you, done with a pen.
The window, with one check missing
The longest stretch with no repeated character, from Problems on text, keeps a window from left to right and a map last of where each character was last seen. When the character at right repeats one inside the window, left jumps past the earlier copy. Here is a version with the guard on its condition missing: it asks only whether the character was seen before, not whether that copy is inside the window.
def longest_unique(text): last = {} # character -> last position seen left = 0 best = 0 for right, ch in enumerate(text): if ch in last: left = last[ch] + 1 last[ch] = right best = max(best, right - left + 1) return best
print(longest_unique("abba"))3
import java.util.*;
public class Main { static int longestUnique(String text) { Map<Character, Integer> last = new HashMap<>(); // character -> last position seen int left = 0; int best = 0; for (int right = 0; right < text.length(); right++) { char ch = text.charAt(right); if (last.containsKey(ch)) { left = last.get(ch) + 1; } last.put(ch, right); best = Math.max(best, right - left + 1); } return best; }
public static void main(String[] args) { System.out.println(longestUnique("abba")); }}3
The right answer for abba is 2: ab or ba. Here is the dry run, one row per pass. The length is right − left + 1.
| right | ch | left | length | best |
|---|---|---|---|---|
| 0 | a | 0 | 1 | 1 |
| 1 | b | 0 | 2 | 2 |
| 2 | b | 2 | 1 | 2 |
| 3 | a | 1 | 3 | 3 |
- Row 0: a is new. The window is a, length 1.
- Row 1: b is new. The window is ab, length 2.
- Row 2: b was seen at 1, so left jumps to 2. The window is b, length 1. Correct so far.
- Row 3: a was seen at 0. The condition is true, so left becomes 0 + 1 = 1. But left was already 2: it has moved back. The window from 1 to 3 is bba, which holds a repeat, and best becomes 3.
The row where a value goes the wrong way is the bug. The earlier a, at position 0, was already outside the window. With the guard, last[ch] >= leftlast.get(ch) >= left, row 3 asks whether 0 is at or after 2. It is not, so left stays at 2, the length is 2 and best stays 2.
Step through both versions below and compare your rows with the table.
- Run the programRun it and step through. Write down left and best after each pass of the loop, one row per pass, and compare your rows with the table above. Watch what left does on the last pass.
def longest_unique(text): last = {} # character -> last position seen left = 0 best = 0 for right, ch in enumerate(text): if ch in last: left = last[ch] + 1 last[ch] = right best = max(best, right - left + 1) return best
print(longest_unique("abba"))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
- Run the programRun the fixed version and step through the last pass. The condition now also asks whether the earlier a is inside the window. Is it?
def longest_unique(text): last = {} # character -> last position seen left = 0 best = 0 for right, ch in enumerate(text): if ch in last and last[ch] >= left: left = last[ch] + 1 last[ch] = right best = max(best, right - left + 1) return best
print(longest_unique("abba"))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Choosing the input
The buggy version also gives 6 for streets, where the right answer is 4. But that dry run takes seven rows, and abba finds the same bug in four. The rule: pick the smallest input that makes each branch of the code run. The guard is for one case, a repeat whose earlier copy is outside the window, so the best input is the shortest one that has that case. An input with no repeats, such as abc, never runs the line that moves left at all, so a dry run on it cannot find a bug there.
In an interview, say what you are doing: "Let me dry-run this on abba, because it has a repeat outside the window." Then fill in the rows out loud.
Check yourself
In the run without the guard on abba, what do left and best hold after the last pass of the loop (right = 3)?
Complete the condition so that left moves only when the earlier copy of the character is inside the window. With it, the program prints 2 for abba.
def longest_unique(text): last = {} # character -> last position seen left = 0 best = 0 for right, ch in enumerate(text): ____________________ # ← the condition, with the guard left = last[ch] + 1 last[ch] = right best = max(best, right - left + 1) return best
print(longest_unique("abba"))import java.util.*;
public class Main { static int longestUnique(String text) { Map<Character, Integer> last = new HashMap<>(); // character -> last position seen int left = 0; int best = 0; for (int right = 0; right < text.length(); right++) { char ch = text.charAt(right); ____________________ // ← the condition, with the guard left = last.get(ch) + 1; } last.put(ch, right); best = Math.max(best, right - left + 1); } return best; }
public static void main(String[] args) { System.out.println(longestUnique("abba")); }}You want to dry-run the window code by hand to find out whether the guard is needed. Which input is the best choice?
Honesty notes
- Dry-run a loop by hand: one column per changing variable, one row per pass.
- Find the bug in the window without its guard: on abba, left moves back from 2 to 1 and best becomes 3 instead of 2.
- Write the guarded condition, which moves left only when the earlier copy is inside the window.
- Choose the smallest input that runs the branch you are unsure about: abba in four rows rather than streets in seven.