Data Structures & Algorithms / Module 10: Correctness and reviewing AI code
Topic 10.4
Practice: find the bug in AI code
Practice finding bugs in AI-written code: loop bounds, edge cases and hidden costs.
Watch: the search stops one step too early and misses the last number. The difference is a single character in the code.
- moving now
- being checked
- no longer used
The review checklist
- Loop bounds:
<or<=? Are the first and last elements checked? See Loop invariants (10.1). - Edge cases: empty, one, zero, negative, duplicates, very large. See Edge cases (10.2).
- Hidden costs:
includes,indexOfor a query inside a loop? See Hidden costs (0.5) and N+1 queries (B1). - Data structures: an array used for lookups that should be a Set or Map? See Hash sets (2.4).
- Hidden assumptions: does the code assume the data is sorted, unique, or never null?
- Numbers: money in floats, big IDs as numbers, division by zero? See Floating point (10.3).
Practice 1: binary search
This is the code from the figure. It works for most targets. Where's the bug?
Code, for programmersWritten by AI
function indexOf(sorted, target) {
let lo = 0, hi = sorted.length - 1;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (sorted[mid] === target) return mid;
if (sorted[mid] < target) lo = mid + 1;
else hi = mid - 1;
}
return -1;
}Show the answer
When the range is down to one element (lo === hi), the loop stops without checking it. A target in that spot is never found, including in a one-element array. The fix: while (lo <= hi). The invariant ("if the target exists, it's between lo and hi, inclusive") shows right away that a one-element range still needs checking.
Practice 2: removing duplicates
Code, for programmersWritten by AI
function unique(emails) {
const out = [];
for (const e of emails) {
if (!out.includes(e)) out.push(e);
}
return out;
}Show the answer
The result is right, but out.includes scans the whole array on every pass: O(n²). For 100,000 emails, that's about 5 billion comparisons. Use a Set: [...new Set(emails)], O(n). There's also an edge-case question: are A@x.com and a@x.com the same? This code treats them as different.
Practice 3: pagination
Code, for programmersWritten by AI
// page starts at 1
function pageOf(items, page, size) {
return items.slice(page * size, page * size + size);
}Show the answer
Off-by-one: the comment says pages start at 1, but page * size assumes they start at 0. Page 1 skips the first size items, which never appear. The fix: (page - 1) * size. Also check: page 0, a negative page, size 0.
Practice 4: a cart total
Code, for programmersWritten by AI
function cartTotal(items) {
let total = 0;
for (const it of items) total += it.price * it.qty; // price like 19.99
return Math.round(total * 100) / 100;
}Show the answer
Prices are stored as floats, so the total carries small errors. Math.round(x * 100) / 100 hides them almost always, but not always: for example 1.005 * 100 is 100.49999999999999, so it rounds down. Store prices as cents (whole numbers) from the start, and the total is exact with no rounding at all.
Quiz
The AI's code passes every example in the prompt. The most useful next step?
while (lo < hi) in an inclusive binary search most likely fails when…
if (!out.includes(e)) out.push(e) inside a loop. What's the problem?
0 of 3 answered.
Built on
Used in
No follow-up topics yet.