cs-046 — The Phantom Zero
| Field | Value |
|---|---|
| id | cs-046 |
| difficulty | Foundational |
| topics | debugging, sentinels |
| lecture | lecture-12 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | lectures 1-2 (problem framing, precise pseudocode) |
| objective | recognise the pattern, translate it into precise steps, and trace it correctly |
| solution | held by the instructor; discussed after the attempt |
Problem
A sentinel value of 0 corrupts statistics for legitimate zeros in the data. Diagnose the sentinel choice and redesign the loop's exit.
Your deliverable is the minimal failing input, the ranked hypotheses, the located mechanism, the minimal fix, and a regression test. If the problem is ambiguous anywhere, state the assumption you are making and carry on — a stated assumption is part of a good answer.
Examples
- Input: readings [0, 5, 0, 7] with sentinel 0 termination
- Output: sentinel stops at first 0: stats over [] or [partial]; real zeros miscounted - fix: read count first, then iterate
Solve for the worked instance first. Then ask what would change if one number, name, or condition were replaced by something stranger.
What to notice
- The sentinel value collides with legal data. Redesign the loop's exit, not the data.
- A bug is a claim about your mental model; the failing input is the experiment.
- The fix should be smaller than the explanation of the bug.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Which hypothesis did you rank first, and what evidence moved it down the list?"
Constraints and assumptions
- Difficulty: Foundational (early-semester techniques assumed: precise statements, traces, simple loops).
- Topics: debugging, sentinels.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes; naming the pattern and stating the assumption is already a pass, completing the calculation is the stretch.
- Optional stretch (not required): describe an alternative approach and say when it beats the obvious one.
Hints and extension
- First nudge: identify the pattern the worked instance uses before computing anything.
- If stuck after three minutes, write the input/output line you expect and trace the smallest example by hand.
- After the reveal, attempt the extension below before the next lecture.
- Extension challenge: Write the regression test that would have caught the defect earliest.