How To Fix the Binary Search Overflow Bug
The overflow bug is a ticking time bomb
Me: Hey, grandma, I’d like to let you know of a bug out there that many computer programmers aren’t aware of
Grandma: What bug is this?
Me: It’s a bug involving binary search.
Grandma: Mmmh, sounds interesting. The last time we went through a binary search lesson, it seemed simple and straightforward.
Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky
— Donald Knuth
As a computer programmer, you may have written code for the binary search algorithm numerous times when writing code for the applications you build or during coding interviews.
However, just like many programmers out there, the code you wrote most probably contains a bug that you’ve never paid attention to. This is because it is highly likely that even the college professor who taught you the Data Structures & Algorithms class is not aware of the bug, and continues to teach this to thousands of students, year in, year out.
Grandma: Yikes! This means thousands of programmers out there are writing buggy binary search code!