Checking Pseudocode Against Worked Examples
Before implementing an algorithm, trace a small example that exercises its update rules, boundaries, and output interpretation.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Pick a separating input
A first-index search in 2, 4, 4, 9 for target 4 should return the first 4 when that is the promise. A no-duplicate input cannot verify this behavior. Reading pseudocode exposes which position rule was stated.
Use an independent answer
Write the correct answer before following the procedure. Trace the variables and look for the first mismatch. A test with distinguishable results often becomes a regression case after implementation.[1]
Choose a trace that separates two plausible interpretations
A procedure seeking the first repeated key usually returns a value when it sees that value again. On [A, B, B, A] this returns B. A procedure seeking the earliest position whose value repeats anywhere would return A. A trace should distinguish these definitions. See first-match scanning.
Seen before position 0: {}
Before position 1: {A}
Before position 2: {A,B}
At position 2, B is already present
Audit inputs on which a shortcut might return early
Read initialization and the stopping condition, then test empty input and input with all distinct values. Example traces can expose unclear pseudocode, while a loop invariant justifies the claimed behavior for every input. See a worked loop invariant.