Sub topic 10
Picking Inputs Worth Tracing
This chapter assumes you can already write 1-based pseudocode with loops and conditionals, and that you can build a trace table that records variable values step by step. It covers the question that comes before any trace: which input should you trace?
Why the Input Matters
A trace shows what an algorithm does for one specific input. If the trace gives the right answer, you have shown the algorithm works for that input and nothing more. Most logic errors show up only for certain inputs, such as an empty array, a value at the very end, or a run of negative numbers. If you only trace a "nice" input, these bugs never run, so the trace never reveals them.
Hand tracing is slow, so you can afford only a few traces. The skill is to pick a small set of inputs that together run every path through the algorithm and test every boundary. A good set of trace inputs works the same way as a good set of unit tests, except that you run it with a pencil.
A Bug That Typical Inputs Hide
Here is a variant of FindMax with a common mistake. The running maximum starts at 0 instead of at the first element.
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 FUNCTIONTrace it with the input from the earlier chapter, A = [3, 7, 2, 9], and it returns 9, which is correct. You could trace a dozen arrays of positive numbers and never see a problem. Now trace an input picked on purpose to stress the initial value:
Input: A = [-5, -2, -8], n = 3 Expected: -2
step | i | A[i] | A[i] > max | max
-----+---+------+------------+----
init | - | - | - | 0
1 | 1 | -5 | false | 0
2 | 2 | -2 | false | 0
3 | 3 | -8 | false | 0
Returns 0, expected -2The bug was there from the start. Only an input built to break the assumption "the answer is at least 0" brings it out. The rest of this chapter is a method for finding such inputs on purpose instead of by luck.
Categories of Inputs
Sort candidate inputs into categories. Each category targets a different kind of mistake.
| Category | What it tests | Typical bug it exposes |
|---|---|---|
| Typical case | Normal behavior on a small, ordinary input | Basic logic errors |
| Smallest valid size | Loops that run zero or one time | Wrong loop bounds, uninitialized results |
| Empty or degenerate input | Behavior outside the normal size range | Reading `A[1]` when `n = 0` |
| Answer at first position | Initialization step | Loop starting one index too late |
| Answer at last position | Final iteration | Loop ending one index too early |
| Duplicates or all-equal values | Strict vs. non-strict comparisons | `>` vs `≥` errors, unstable results |
| Extreme values | Sign and range assumptions | Sentinel values like 0 or -1 that can occur in real data |
| Already sorted / reverse sorted | Best and worst ordering | Inner loops that never run or always run fully |
You do not need a separate trace for every row. One input can cover several categories at once. For example, [9, 9, 4] puts the maximum first and also includes a duplicate.
Boundaries: Zero, One, Many
Loops cause most tracing errors, and each loop has three counts worth testing: zero iterations, one iteration, and more than one. Find the input that makes each loop run each of those counts.
For the summing loop from the earlier chapters (WHILE i ≤ n, starting at i ← 1):
| n | Iterations | Expected result |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 1 | 1 |
| 3 | 3 | 6 |
The n = 0 row checks that the loop condition fails right away and the initial value of total is the correct answer. The n = 1 row checks that the body runs exactly once. The n = 3 row is the typical case. If the condition were i < n, the n = 1 trace would return 0 instead of 1. That trace exposes the off-by-one in just two lines of work.
Covering Every Branch
Selection adds paths. Every IF should be traced at least once with its condition true and at least once with it false. In the trace table above for FindMax, the column A[i] > max makes this easy to check: scan down the column and confirm both true and false appear. If a column only ever shows one value, your input never tests the other branch.
For an IF with a compound condition such as i ≥ 1 AND A[i] > key in insertion sort, check each way the condition can become false:
A[i] > keybecomes false first: the key belongs in the middle of the sorted prefix.i ≥ 1becomes false first: the key is smaller than everything before it and moves to position 1.
An input like [5, 2, 4] covers both. When key = 2 it slides all the way to the front, so i reaches 0. When key = 4 it stops after passing 5.
Keeping Trace Inputs Small
Each extra element adds rows to the trace table. Use the smallest input that still reaches the case you want:
- Two elements are enough to test a swap or a comparison.
- Three elements are usually enough to put a value at the first, middle, and last positions.
- Four or five elements are rarely needed, except for nested loops where you want the inner loop to run different numbers of times.
Use small, distinct numbers whose relationships you can see at a glance. Use repeated values only when you are deliberately testing duplicates. An input like [30, 10, 20] is easier to follow than [1047, 382, 915]. It is also easier than [1, 1, 1], which hides which element moved where.
A Procedure for Choosing Inputs
The first step matters more than it looks. FindMax assumes a non-empty array. Tracing it with n = 0 would read A[1], which does not exist. That is not a bug in FindMax. It is a call that breaks the precondition. Note the precondition, then decide whether the caller must guarantee it or whether the algorithm should check for it. In both cases, the empty-input trace is what makes you face that decision.
After you fix a bug, retrace all the inputs, not only the one that failed. A fix for the last-position case can easily break the single-element case.
Applying the Method to FindMax
Here is a full input set for the correct FindMax, with max ← A[1] and the loop running from 2 to n:
| Input | Categories covered | Expected |
|---|---|---|
| `[3, 7, 2, 9]` | Typical, answer last, both branches | 9 |
| `[8]` | Smallest size, loop runs zero times | 8 |
| `[9, 4, 6]` | Answer first, IF never true | 9 |
| `[-5, -2, -8]` | All negative, answer in middle | -2 |
| `[4, 4]` | Duplicates, equality boundary | 4 |
That is five short traces. Together they would catch a 0-initialized maximum, a loop starting at 1 or 3, a loop ending at n - 1, and a ≥ that should be > (here a ≥ gives the same value, but in a version that returns the index it would decide whether the first or last match is reported).
Exercises
Exercise
A linear search returns the index of the first element equal to target, or 0 if none is found. Its loop is FOR i ← 1 TO n, with IF A[i] = target THEN RETURN i, and RETURN 0 after the loop. List a minimal set of trace inputs (array and target) with expected outputs that covers the categories in this chapter.
Show answer
One reasonable set: A = [] (n = 0), target 5 → 0 (loop runs zero times). A = [5], target 5 → 1 (single element, found). A = [5], target 3 → 0 (single element, not found). A = [5, 8, 2], target 5 → 1 (found at first position). A = [5, 8, 2], target 2 → 3 (found at last position, catches a loop ending at n - 1). A = [4, 7, 4], target 4 → 1 (duplicates, checks that the first match is returned). A = [5, 8, 2], target 9 → 0 (not found in a longer array, IF always false).
Exercise
Someone traced InsertionSort only on A = [1, 2, 3] and A = [2, 1, 3] and concluded it is correct. What paths through the algorithm did these traces never test, and which single 3-element input would test them?
Show answer
In [1, 2, 3] the inner WHILE never runs. In [2, 1, 3] it runs once, for key = 1, and stops because i drops below 1, but 3 never moves. Neither trace has a key that stops partway because A[i] > key becomes false after at least one shift, and the inner loop never runs more than once. A reverse-sorted input such as [3, 2, 1] makes the inner loop run once and then twice, each time ending with i reaching 0. To also test the stop-in-the-middle exit, add [2, 3, 1] or [3, 1, 2]. For [3, 1, 2], key = 2 shifts 3 and then stops at A[1] = 1.
Exercise
Why is [-5, -2, -8] a better trace input for FindMax than [-5, -8, -2]?
Show answer
Both are all-negative and would expose a 0-initialized max. [-5, -2, -8] also puts the answer in the middle, so the IF is true once and then false afterward, and the trace checks that a later, smaller value does not overwrite max. [-5, -8, -2] puts the answer last, which the typical input [3, 7, 2, 9] already covers.