Off by One Errors and Edge Cases

Topics Covered

Why Boundaries Break

Where the assistant gets boundaries from

Three common shapes

Why tests miss them

A first example in Python

A second example in TypeScript

How to look for them

A drill for spotting boundaries

Boundaries hide in words

Why the assistant's comments do not help

Boundaries in the round

What the interviewer hears

The one question to ask

Off by one errors are the most common bug in assistant code, and the most common bug in human code too. They happen when code treats a boundary value as inside a range when it should be outside, or outside when it should be inside. They rarely show on typical inputs. They show only on the exact boundary, which is why they slip through review and through tests written from typical examples.

Most off by one bugs are a boundary treated as inside when it should be outside, or the reverse.

Where the assistant gets boundaries from

An assistant decides whether to write less than or less than or equal from patterns in the code it has seen and from the words in your prompt. If your prompt says the booking must not overlap, it has to guess what overlap means at the edges. Different codebases use different conventions, so its guess is a coin toss weighted by whatever was most common in its training data. It does not know your README says bookings are half open unless you tell it or it reads the README, and even then it may not apply the convention consistently.

That means the comparison operator in an assistant diff is a guess until you have checked it against the rule. Treat every comparison in the details pass as a question: which side of the boundary does this put the edge value on, and is that what the rule says?

Three common shapes

Off by one errors come in three common shapes.

The first is the wrong comparison: less than where less than or equal is needed, or the reverse. This decides whether the exact boundary value is included.

The second is the wrong range: a loop that runs from zero to n minus one when it should run to n, or a slice that stops one element short. This decides whether the first or last item is processed.

The third is the wrong adjustment: adding or subtracting one where the rule does not call for it, often when converting between inclusive and exclusive ranges, between zero based and one based indices, or between lengths and last indices.

Why tests miss them

Tests miss off by one errors because tests are usually written from examples, and examples are usually typical. A booking from ten to twenty and another from thirty to forty test the common case. Only a booking from ten to twenty followed by one from twenty to thirty tests the boundary. In interview codebases, the visible tests are often written to pass with the starter code, so if the starter code has a boundary bug, the visible tests probably do not touch that boundary.

That is the core insight of this lesson. If a function has a boundary, and the visible tests do not include a case exactly at that boundary, then the visible tests passing tells you nothing about whether the boundary is right.

A first example in Python

Here is a diff an assistant might propose for a function that returns the items on a given page, with pages numbered from one.

diff
1 def page_items(items, page, page_size):
2-    return items[page * page_size:(page + 1) * page_size]
3+    # Pages are 1-indexed.
4+    start = page * page_size
5+    return items[start:start + page_size]

The comment says pages are one indexed, but the code is still zero indexed: page one starts at page size, so the first page of results is skipped. The diff tidied up the slice and added a comment that makes the bug look fixed. The correct start is page minus one times page size. A probe with page one, which should return the first page size items, exposes it immediately.

A second example in TypeScript

diff
1 function lastN<T>(items: T[], n: number): T[] {
2-  return items.slice(items.length - n);
3+  const out: T[] = [];
4+  for (let i = items.length - n; i < items.length - 1; i++) out.push(items[i]);
5+  return out;
6 }

The original slice was correct for n up to the length. The rewrite stops one short: the loop condition should be i less than items length, not items length minus one, so the last item is always missing. It also changed behaviour when n is larger than the length, since a negative start index now reads undefined values instead of clamping. Two off by one problems in a three line rewrite, neither visible on a quick read.

How to look for them

In the details pass, look at every comparison, every range, every slice and every plus or minus one. For each, ask: what happens at the exact edge? Then probe the edge. The rest of this lesson gives you the conventions to check against and a short list of edges to probe.

A drill for spotting boundaries

Take a function you wrote recently that has a range or comparison, and for each boundary in it, write down what should happen at the exact edge according to its specification. Then ask an assistant to rewrite the function in a different style and check each boundary in its version. Doing this a handful of times trains you to see every comparison operator as a question rather than a detail.

Boundaries hide in words

Many boundaries are written in words before they are written in code. At least, at most, more than, fewer than, before, after, up to, until, within and between all define an edge, and each one has an exact meaning that decides a comparison operator. Before reading code, underline these words in the task or README. Each one is a boundary you will need to check, and each one maps to a specific operator: at least is greater than or equal, more than is strictly greater, up to often means up to and including, and until is famously ambiguous.

When a word is ambiguous, such as between or until, ask. Is a booking until noon still active at noon? Is a value between one and ten allowed to be ten? These questions sound pedantic, but in an AI-assisted round they show exactly the care the interviewer is looking for, and they settle comparisons that the assistant would otherwise guess.

Why the assistant's comments do not help

Assistant diffs often include a comment above a comparison, such as inclusive end or exclusive of the boundary. These comments are generated from the same guess as the code, so they agree with the code whether or not the code agrees with the rule. A comment saying the end is exclusive above a less than or equal comparison is a contradiction worth spotting, but a comment that matches a wrong comparison is just as wrong as the comparison. Read the operator, not the comment.

Boundaries in the round

In a typical AI-assisted round, boundaries come up in the fix stage, where a bug is often a single wrong operator, in the extend stage, where a new feature adds a new threshold or range, and around the twist, which often changes a limit or a convention. Each time a boundary appears, the same habit applies: find the rule, probe the edge and check the operator.

What the interviewer hears

When you say I am going to check the exact boundary, the interviewer hears that you know where bugs live. When you then find one, they hear that your instinct was right. When the probe passes, they still hear that you checked. A spoken boundary check is one of the cheapest and most reliable positive signals in the round.

The one question to ask

If you remember one question from this section, make it this one: what happens at exactly the edge? Ask it of every comparison, every range and every loop in a diff, and probe whenever the answer is not obvious from the rule.

Boundaries are small, but they decide correctness more often than any other single detail in interview code.

A comparison operator is a guess until checked

The assistant chooses less than or less than or equal from patterns, not from your rule. Check every comparison in a diff against the rule, and probe the exact boundary value.