4 ms·
Well, but he said "forgot how to write binary search in 10 minutes". This came up here three months ago: "school was years ago...I've forgotten how to implemen
by kragen 5y ago
Well, but he said "forgot how to write binary search in 10 minutes". This came up here three months ago: "school was years ago...I've forgotten how to implement a hash table in C within 30 minutes. Is that really the gatekeeper we want?"
Well, I thought, yes, I think it is, actually? So I tried implementing a hash table in C—without testing it, as if I were writing it in an interview without programming tools. It took me 15 minutes and had a significant bug: https://news.ycombinator.com/item?id=26593250 https://news.ycombinator.com/item?id=26593250
I concluded that implementing a hash table in C in 30 minutes is a reasonable thing to ask someone to try during an interview, and how people work on it will probably tell you a lot about their programming abilities. I wouldn't hire them for a C programming job if they said "school was years ago, I've forgotten", but you shouldn't necessarily expect them to succeed flawlessly.
Binary search in particular is notoriously tricky. It's easy to explain the idea in a lot less than 10 minutes, and it's easy to write a version of the code that sometimes works in less than 10 minutes, something like
bs(k, a, i, j)
{
int m = (i+j)/2;
return a[m] == k ? m :
a[m] < k ? bs(k, a, m, j) :
bs(k, a, i, m);
}
But it's easy for the algorithm to hide subtle bugs. That version has at least one type error (in modern C, anyway), one obvious correctness bug, at least one obvious performance bug, and probably some subtle correctness bugs as well. Many years passed between the first publication of a binary search algorithm and the first publication of a correct one.
Also, though, there are lots of kinds of programmers. You can spend a lot of time writing screen-scrapers or CRUD or machine learning models in Python without ever needing to implement binary search. In Python you should probably just use the bisect module in practice, most of the time, rather than reimplementing it.
- otabdeveloper4 5y ago> But it's easy for the algorithm to hide subtle bugs. For sure, but I guarantee you the interviewer from the grandparent comment wasn't looking for correctness and safety when they asked to implement a binary search. They ask the binary search question to check if the applicant knows what an algorithm is and if they ever had to implement one. (Any algorithm.) Sadly, 90% of programmers these days don't and haven't. > In Python you should probably just use the bisect module in practice, most of the time, rather than reimplementing it. Well, yes, most developers ship software without ever having to actually program.
- kragen 5y ago> I guarantee you the interviewer from the grandparent comment wasn't looking for What, the company that interviewed James Hush about binary search was your company? Have you thought about the possibility that maybe he also interviewed at at least one other company which used different evaluation criteria? Maybe you should put a little bit more effort into correctness yourself! > They ask the binary search question to check if the applicant knows what an algorithm is and if they ever had to implement one. (Any algorithm.) > Sadly, 90% of programmers these days don't and haven't. That makes no sense. Every program or subroutine implements an algorithm. If you haven't written any programs or subroutines you aren't a programmer. > Well, yes, most developers ship software without ever having to actually program. This reminds me of when I was a kid and we thought it wasn't "actually programming" when we programmed in BASIC or Pascal because actual programs were written in assembly. We were wrong about that.