Trace code with a variable table and find a bug from the first failing input.
A bug sits where the code and a hand trace first disagree, so shrink the input until that first disagreement is one row of a table.
Use it when the output is wrong and you do not yet know why. Signal words: my answer is off by one, it works on the example but not on the test, which input makes it fail.
Start: find a failing input, and write the correct answer by hand or from a brute force.
Step: shrink the input while it still fails. Trace the small input by hand, one row for each step, and compare each row with the code.
Stop: you can point at one row where the code does something the trace does not.
It does not find a bug that appears only at a large size, such as a total that wraps. For those, check the largest input by counting.
Cost: a hand trace of a small input takes minutes. Printing every step of a large input can print millions of lines.
def count_rises(readings):
# [1] No rise has been seen yet.
rises = 0
# [2] Start at place 1: place 0 has no reading before it.
for i in range(1, len(readings)):
# [3] A rise: this reading is higher than the one just before it.
if readings[i] > readings[i - 1]:
rises += 1
# [4] Return the number of rises.
return risespublic int countRises(int[] readings) {
// [1] No rise has been seen yet.
int rises = 0;
// [2] Start at place 1: place 0 has no reading before it.
for (int i = 1; i < readings.length; i++) {
// [3] A rise: this reading is higher than the one just before it.
if (readings[i] > readings[i - 1]) {
rises++;
}
}
// [4] Return the number of rises.
return rises;
}int countRises(const std::vector<int>& readings) {
// [1] No rise has been seen yet.
int rises = 0;
// [2] Start at place 1: place 0 has no reading before it.
for (size_t i = 1; i < readings.size(); i++) {
// [3] A rise: this reading is higher than the one just before it.
if (readings[i] > readings[i - 1]) {
rises++;
}
}
// [4] Return the number of rises.
return rises;
}function countRises(readings) {
// [1] No rise has been seen yet.
let rises = 0;
// [2] Start at place 1: place 0 has no reading before it.
for (let i = 1; i < readings.length; i++) {
// [3] A rise: this reading is higher than the one just before it.
if (readings[i] > readings[i - 1]) {
rises++;
}
}
// [4] Return the number of rises.
return rises;
}def first_difference(mine, expected):
# [1] Compare only as far as the shorter list reaches.
shorter = min(len(mine), len(expected))
# [2] Report the first place where the values differ.
for i in range(shorter):
if mine[i] != expected[i]:
return i
# [3] Equal so far: if the lengths differ, the difference starts where the shorter list ends.
if len(mine) != len(expected):
return shorter
# [4] The lists are identical.
return -1public int firstDifference(int[] mine, int[] expected) {
// [1] Compare only as far as the shorter list reaches.
int shorter = Math.min(mine.length, expected.length);
// [2] Report the first place where the values differ.
for (int i = 0; i < shorter; i++) {
if (mine[i] != expected[i]) {
return i;
}
}
// [3] Equal so far: if the lengths differ, the difference starts where the shorter list ends.
if (mine.length != expected.length) {
return shorter;
}
// [4] The lists are identical.
return -1;
}int firstDifference(const std::vector<int>& mine, const std::vector<int>& expected) {
// [1] Compare only as far as the shorter list reaches.
int shorter = (int)std::min(mine.size(), expected.size());
// [2] Report the first place where the values differ.
for (int i = 0; i < shorter; i++) {
if (mine[i] != expected[i]) {
return i;
}
}
// [3] Equal so far: if the lengths differ, the difference starts where the shorter list ends.
if (mine.size() != expected.size()) {
return shorter;
}
// [4] The lists are identical.
return -1;
}function firstDifference(mine, expected) {
// [1] Compare only as far as the shorter list reaches.
const shorter = Math.min(mine.length, expected.length);
// [2] Report the first place where the values differ.
for (let i = 0; i < shorter; i++) {
if (mine[i] !== expected[i]) {
return i;
}
}
// [3] Equal so far: if the lengths differ, the difference starts where the shorter list ends.
if (mine.length !== expected.length) {
return shorter;
}
// [4] The lists are identical.
return -1;
}This approach reads the loop three times and finds nothing.
This version subtracts 1 from the answer, because the example gave 3 and should give 2.
This approach runs the one example and stops.
Tracing small inputs finds logic errors. It does not find a total that wraps. Adding three payments of 1,000,000,000 in a 32-bit integer gives -1,294,967,296, and no list of three small numbers shows it.
systemdesignmastery.dev/dsa/cheatsheets/hand-tracing-and-debugging