Off-by-One Errors

Common mistakes in boundary conditions.

Mistake 1: Using high = arr.length - 1 for lower bound.

Lower bound can return arr.length if all elements are smaller. Initialize high = arr.length.

Mistake 2: Using while low <= high for lower bound.

Lower bound converges when low == high. Using <= causes extra iterations or wrong answers.

Mistake 3: Returning mid instead of low after the loop.

For lower bound, the answer is low (which equals high) after the loop ends.

Debugging tip: Trace through with a 22-element array like [1,3][1, 3] searching for 00, 11, 22, 33, and 44. Cover all edge cases.