š WAYPOINT LESSON
The Minimal Counterexample
āāā advancedā³ 13 min readš Lesson 241 of 281
Shrink any counterexample to its skeleton: the minimal case is both the proof of wrongness and the specification of the fix.
The Minimal Counterexample
Given any failing input, shrink it. Every step of shrinking either keeps the failure (good ā smaller) or breaks it (better ā you have localized a necessary component of the bug).
The shrink loop
- Delete an element. Still fails? Keep it deleted.
- Halve the values (preserve order/parity relationships that matter).
- Merge adjacent equal elements.
- Replace large values with 3-style stand-ins, preserving only the comparisons the solution makes.
Stop when the case is irreducible ā typically n ⤠5. The irreducible case states the bug as a theorem: for this input shape, this code outputs X, correct is Y.
Why minimal matters
- Diagnosis: the minimal case usually fits on one screen; the offending line is visible.
- The fix spec: the minimal case says exactly which structure the code mishandles ā the fix must handle that shape, and the case becomes the regression test.
- Communication: "this fails on [3, 1, 2]" is a complete bug report; "it fails on my 200-line random test" is not.
When nothing small fails
If n ⤠5 never fails but n = 40 does, the bug is either scale-dependent (overflow, O(n²) death) or structural (needs ā„ k items to interact). Then build the structured minimal case: k = the smallest interacting count, with a repeating pattern ā not random bulk.
ā” Now practice
Ready to CodeAdversarial DrillsThree drills: the tie blind spot, the deletion shrink loop, and structured minimal cases for scale-dependent bugs.
3 challenges Ā· Ā· ~20 min