Skip to content
RED SAG Skills
hard20 pts · first answer countsarraysbinary-searchdebugging

Binary search that never ends

This search for the last index with a[i] <= target loops forever on some inputs. Why?

while (lo < hi) {
  const mid = Math.floor((lo + hi) / 2)
  if (a[mid] <= target) lo = mid
  else hi = mid - 1
}
Choose one answer

Discussion 0

Join free to post in the discussion.

Related questions