I hope you're not trying to imply that coding a binary search is challenging for most of your interviewees. :-)
I hope you're not trying to imply that coding a binary search is challenging for most of your interviewees. :-)
"Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky…" — Professor Donald Knuth
When Jon Bentley assigned it as a problem in a course for professional programmers, he found that an astounding ninety percent failed to code a binary search correctly after several hours of working on it, and another study shows that accurate code for it is only found in five out of twenty textbooks (Kruse, 1999). Furthermore, Bentley's own implementation of binary search, published in his 1986 book Programming Pearls, contains an error that remained undetected for over twenty years.
http://en.wikipedia.org/wiki/Binary_search_algorithm#The_alg...
I do want to see a candidate speak about its complexity (demonstrate it at least intuitively), and about the impact that the partitioning function has.
But these are things any CS college student in his second year should know about (otherwise they've wasted their time in college, and frankly I haven't seen many of those).
I'd really wish we'd stop with the "mine is bigger than yours attitude" ... there are lots of good developers out there. They are just more specialized in other domains, and finding the right match is indeed hard ... but the faulty interviews we are so accustomed with are to be blamed.
if (a[i] < a[j])
That says that that array is completely loaded in your address space. :-)In Java for example, an "int" is 32 bits, even on 64 bits machines/VMs ... where the address space can reach 16.3 million terabytes.
So this little bug can byte, but only if you're manipulating large datasets in memory. And then it's easily discoverable, on top of managed languages anyway (since the index used on an array can't be negative).
Also, the page provided by sanj contains a recount by a Google employee where he and most of his fellow CS Ph.D. students at CMU were unable to provide a correct implementation of binary search.
Sometimes smart people make dumb mistakes, especially when rushed or stressed.
http://googleresearch.blogspot.com/2006/06/extra-extra-read-...