๐ŸŽฏ DSA

Binary Search โ€” Why Everyone Gets It Wrong (Off-By-One)

๐Ÿ“… Jul 2, 2026 โฑ 4 min read

Binary search is 10 lines, invented in 1946 โ€” and studies found most programmers write it with bugs. The bugs are always boundaries.

The template that never fails

function search(nums, target) {
  let lo = 0, hi = nums.length - 1;
  while (lo <= hi) {                        // โ‰ค not < !
    const mid = Math.floor((lo + hi) / 2);
    if (nums[mid] === target) return mid;
    if (nums[mid] < target) lo = mid + 1;   // +1, or infinite loop
    else                    hi = mid - 1;   // -1, or infinite loop
  }
  return -1;
}

The three bug factories

It's more than finding values

Variants: first/last occurrence, rotated sorted array, "binary search the answer" (min capacity, min speed problems). All the same skeleton with a different condition.

Test your version against our hidden test cases โ€” the empty array and 2-element cases catch most implementations.

โ† All Articles