Common Mistakes

Common errors to avoid

  • Off-by-one in loop condition: Use left <= right for standard search, left < right for boundary finding. Getting this wrong causes infinite loops or missed elements.
  • Integer overflow in mid calculation: Use mid = left + (right - left) / 2 instead of (left + right) / 2 to prevent overflow with large indices.
  • Wrong half elimination: When target could be at mid, don't exclude mid from the next search range.

Use right = mid not right = mid - 1 in boundary searches.