Sub topic 11
Desk Checking: Finding Bugs on Paper
A trace table records what an algorithm does. A desk check asks whether that is what the algorithm should do. You run the pseudocode by hand on chosen inputs and compare each result with an answer you worked out independently. When the two disagree, you have found a logic error before writing any real code.
This chapter assumes you can already build a trace table, trace a loop iteration by iteration, and choose inputs that include edge cases. Those skills were covered in earlier chapters. Here you combine them into a repeatable bug-finding procedure.
What a Desk Check Is
A desk check is a manual execution of an algorithm with three parts:
- A specific input.
- An expected output, computed without using the algorithm under test.
- A trace that produces the actual output, step by step.
The order matters. If you trace first and decide afterwards whether the answer "looks right", you will tend to accept whatever the trace gives you. Write the expected result down before you start.
A desk check finds logic errors. A logic error is a mistake that produces wrong results even though every line is valid pseudocode. A compiler cannot catch these. Syntax errors matter less in pseudocode, because no machine reads it.
The Desk-Check Procedure
The step that most people skip is "find the first row that is wrong". A wrong final answer only tells you that a bug exists. The first row where a variable takes an unexpected value tells you where the bug is. Once you know the step, you can usually name the line.
Bug 1: Off-by-One in a Loop Condition
This loop is supposed to return the sum 1 + 2 + … + n.
total ← 0
i ← 1
WHILE i < n DO
total ← total + i
i ← i + 1
END WHILE
RETURN total
Trace with n = 3 (expected 1 + 2 + 3 = 6):
i = 1, total = 0 -> total = 1, i = 2
i = 2, total = 1 -> total = 3, i = 3
i = 3: condition 3 < 3 is false, loop ends
Returns 3, not 6. Fix: change the condition to i ≤ n.The expected value comes from the formula n(n + 1)/2 = 6, which does not depend on the loop. The trace returns 3. The first wrong moment is the condition check when i = 3. The loop should run once more and stops. The comparison operator is wrong.
After the fix, re-check, and include the edge cases:
| n | Expected n(n+1)/2 | Trace with `i ≤ n` | Match? |
|---|---|---|---|
| 3 | 6 | 1, 3, 6 → 6 | yes |
| 1 | 1 | 1 → 1 | yes |
| 0 | 0 | loop never runs → 0 | yes |
The n = 0 row matters. A fix that works for typical inputs can still break the empty case. Here it does not, because 1 ≤ 0 is false straight away.
Bug 2: Wrong Initialization
Here is a variant of FindMax that someone "simplified":
FUNCTION FindMaxBuggy(A, n)
max ← 0
FOR i ← 1 TO n DO
IF A[i] > max THEN
max ← A[i]
END IF
END FOR
RETURN max
END FUNCTIONWith A = [3, 7, 2, 9] it returns 9, which is correct. A desk check that uses only that input passes, and the bug survives. Now try an input where every element is negative, A = [−4, −2, −7]. The expected answer is −2.
step | i | A[i] | A[i] > max | max
-----+---+------+------------+----
init | - | - | - | 0
1 | 1 | -4 | false | 0
2 | 2 | -2 | false | 0
3 | 3 | -7 | false | 0
Returns 0, expected -2The first wrong row is init. The value 0 is not an element of the array, so it should never be a candidate maximum. The fix is to start from a real element: max ← A[1], with the loop starting at 2. This is the original FindMax.
This bug shows why choosing inputs is part of desk checking. The algorithm quietly assumed that some element is at least 0. Only an input that breaks that assumption exposes it.
Bug 3: A Loop That Never Ends
This fragment is meant to count how many times 2 can be subtracted from a non-negative n before reaching 0:
count ← 0
WHILE n ≠ 0 DO
n ← n - 2
count ← count + 1
END WHILE
RETURN countFor n = 4 the trace gives n = 2, then 0, and returns 2, which is correct. For n = 3:
iter | n before | n after | count
-----+----------+---------+------
1 | 3 | 1 | 1
2 | 1 | -1 | 2
3 | -1 | -3 | 3
4 | -3 | -5 | 4You don't need to trace forever. Once n is negative and decreasing, it can never equal 0 again, so the loop never terminates. The bug is that the loop's exit condition tests for an exact value that the update can step past. Changing the condition to n > 0 makes the loop end for every non-negative n. For n = 3 it returns 2, which matches "subtract 2 twice, with 1 left over". Decide whether that is what the problem statement means. A desk check sometimes reveals that the specification was ambiguous, and that discovery is useful too.
A Catalogue of Common Logic Errors
| Error | Typical symptom in a trace | Input that tends to expose it |
|---|---|---|
| `<` instead of `≤` (or the reverse) | Last element skipped, or index past the end | Smallest valid size, such as n = 1 |
| Bad initial value | First row already wrong | All-negative, all-equal, or empty input |
| Missing or misplaced update | Variable never changes, loop repeats | Any input that enters the loop |
| Updates in the wrong order | Value used one step too early or late | Small input traced line by line |
| Wrong variable in an expression | Plausible numbers that are slightly off | Input with distinct, easy-to-tell-apart values |
| Exit test on an exact value | Variable jumps past the target | Odd or non-multiple values |
The last column connects back to input choice. Use values that are easy to tell apart, such as 3, 7, 2, 9 rather than 5, 5, 5. That way a mixed-up variable produces a visibly wrong number.
Confirming a Fix in Real Code
Once the pseudocode passes on paper, you can cross-check it with a short program. The program checks the desk check. It does not replace it.
def sum_to_n_buggy(n):
total, i = 0, 1
while i < n:
total += i
i += 1
return total
def sum_to_n_fixed(n):
total, i = 0, 1
while i <= n:
total += i
i += 1
return total
for n in [0, 1, 3, 10]:
expected = n * (n + 1) // 2
print(n, expected, sum_to_n_buggy(n), sum_to_n_fixed(n))The output shows the buggy version falling short by exactly n for every n ≥ 1, while the fixed version matches the formula. A systematic gap like this is itself a clue. "Always short by the last term" points straight at the loop boundary.
Habits That Make Desk Checks Work
- Write the expected result before tracing, every time.
- Record each variable change as its own row. Don't merge steps in your head.
- Trace the loop's final failing condition check explicitly.
- After any fix, re-run every input you have already checked. A fix for one case can break another.
- Keep the traces. They become test cases when you translate the pseudocode to code.
Exercise
This pseudocode should return the number of elements of A[1..n] that are greater than t. Desk check it with A = [5, 1, 8], n = 3, t = 4, find the first wrong row, and fix it. count ← 0 FOR i ← 1 TO n - 1 DO IF A[i] > t THEN count ← count + 1 END IF END FOR RETURN count
Show answer
The expected answer is 2, because 5 and 8 are greater than 4. The trace goes: i = 1, 5 > 4, count = 1. Then i = 2, 1 > 4 is false, count = 1. The loop ends after i = 2 because the upper bound is n - 1 = 2, so it returns 1. The first wrong point is the loop's end: i = 3 is never visited, so A[3] = 8 is never tested. The fix is FOR i ← 1 TO n. Re-checking gives 2, and n = 0 still gives 0.
Exercise
A student initializes min ← 100 in a FindMin routine and says that every desk check passed. Give an input that exposes the bug, and state the correct initialization.
Show answer
Any array whose elements are all greater than 100 exposes it, for example A = [150, 200]. The routine returns 100, which is not in the array. Initialize with min ← A[1] and start the loop at i ← 2, which works for any non-empty array.
Exercise
In the subtract-by-2 loop, the condition n ≠ 0 was changed to n > 0. Desk check n = 0 and n = 5 and state each expected result before tracing.
Show answer
For n = 0 the expected result is 0. The condition 0 > 0 is false, so the loop never runs and the routine returns 0. For n = 5 the expected result is 2, because 5 = 2 + 2 + 1. The trace goes n = 3 with count 1, then n = 1 with count 2. Then 1 > 0 is true, so n = -1 and count = 3, and the loop ends. It returns 3, which does not match. With the condition n > 0, the loop counts how many steps it takes to reach 0 or below, not how many whole 2s fit into n. If the intended meaning is whole 2s, the condition must be n ≥ 2, which gives 2 for n = 5 and 0 for n = 0.