A few days ago I tried the experiment of implementing quicksort after not looking at a specification or implementation of the algorithm for several years. The experiment started because my friend @FrancescoSciuti was doing developers phone interviews and reported back bad news.
Many candidates had issues at implementing algorithms in a shared screen setting. In order to give myself some metric about how hard actually is to write stuff out of thin air, without looking at anything, I decided to do such quicksort implementation experiment.
Results: to implement quicksort at high level, that is with a partition() function returning two lists, in the form

l,r = partition(vector,pivot)

was trivial. Literally the time to write it and it worked out of the box (incidentally, how beautiful is quicksort? Impressive).
However things are different once you want to do in-place partitioning in C, just with swapping. I had to write a comment in my C file (I had no pencil & paper) in order to re-invent the two indexes starting at start/end, and skipping already sorted elements, than swapping.
It didn't worked well the first time, and in the end I spent around 20 minutes to have a working in-place quicksort implementation written in C, that worked well for my test input. And... the next day I discovered there was even a little bug as edge case.
My take on this is that it's simple to make errors if you didn't look at the algo you are implementing for several years, but that candidates that totally fail to write any reasonable code are suspicious.
I suggested my friend to start with much simpler questions like this: given an array, fill it with random values, than find the min and the max, and swap them inside the array. This is trivial, but can give an hint about the fact the candidate can write any code.
Then incrementally provide another problem. And finally as third step maybe ask to write a sorting algorithm. Btw when asked to write *any* sorting algorithm, IMHO the best algorithm to implement is selection sorting. It is less naive than bubble sort, yet trivial.
TLDR: writing working code without proper analysis and testing is hard, however for an interview, it is sufficient to give enough hints about the fact you *could do it* well, even if the result is not prefect.
You can follow @antirez.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: