Off-by-one and boundary bug hunt with loop invariants and trace tables

by fidji DeepSeek English ~626 tokens
Public excerpt Locked
For each loop and range: invariant, termination argument, then a hand trace on n = 0, 1, exact multiple and multiple + 1. Inclusive or exclusive bounds made explicit, a failing input for each bug, the fix re-traced on the same inputs…

About this prompt

Hunts the bugs that hide at the edges: loops that run once too often or too little, pagination that drops the last items, inclusive versus exclusive ranges, empty inputs, date intervals. The prompt makes DeepSeek state each loop invariant and trace the code by hand on boundary values, so every finding comes with a table you can check line by line rather than a guess.

Example output

Provided by the author
CONTRACT Pages numbered from 1, 10 items per page. total = 45 → 5 pages; page 5 returns items 41–45. BOUNDARY TRACE (original code) pageCount = total / perPage (integer division); offset = page * perPage | total | page | pageCount | offset | Items returned | Expected | | 45 | 1 | 45 / 10 = 4 | 1 × 10 = 10 | 11–20 | 1–10 ✗ | | 45 | 5 | 4 | not reachable | none | 41–45 ✗ | | 40 | 4 | 4 | 40 | none | 31–40 ✗ | | 0 | 1 | 0 | 10 | none | empty page ✓ | FINDINGS 1. Line 3: integer division drops the last partial page. Smallest failing input: total = 1 → 0 pages. 2. Line 4: page is 1-based but used as a 0-based multiplier: page 1 skips items 1–10. FIX pageCount = (total + perPage − 1) / perPage → (45 + 9) / 10 = 54 / 10 = 5 ✓ offset = (page − 1) * perPage → page 1: 0, page 5: 40 → items 41–45 ✓ …

Details

Model
DeepSeek
Prompt language
English
Who it's for
Developers reviewing pagination, batching, slicing, date-range or search code before release
Length
~626 tokens (estimate)
Delivered as
Prompt text, shown online after purchase

Reviews

Buyers only

No reviews yet. Only buyers of this prompt can leave one.

Report this prompt