Hacker News new | ask | show | jobs
by brudgers 4269 days ago
Questions warrant answers commensurate with the effort invested in their construction. "How do you reverse a linked list?" is, on it's face, a waste if time. Beyond being a solved problem:

~ Single or doubly linked?

~ How is the data structure implemented (e.g. is there a header structure and to what does it point)?

~ What does "reverse" mean? mutate in place? create a new list? and if I am to mutate it in place, why?

~ Reversing a linked list is O(n). One implementation us pretty much as good as another. It doesn't really differentiate on insight into the problem.

~ Winding up in a situation where reversing a list is critical smells like bad design...a stack/queue would have been a better choice upstream than a queue/stack.

But though all that may make for interesting conversation, "with a library call" is the only professional answer to "How would you write a function to reverse a list?"

1 comments

> "with a library call" is the only professional answer to "How would you write a function to reverse a list?"

Most interviewers - in my experience - would then mark you down as a smartarse and the interview will be all downhill from there.

I can in fact be a smartass in the presence of bullshit.[1]

I come from a field where there is an ethos of professionalism, and the answer to "How would you reinforce a strip footing?" is "With rebar."

~ Uncle Bob has a point.

~ Half of all interviewers are below average.

[1] "Bullshit" in the technical sense - when a person knowingly states something misleading or false and openly doesn't care if the person hearing those statements believes them.

I'm wholly in agreement with you. I suspect questions like this are used in lieu of a sane programming test. Although I guess they might have relevance if you have no library (embedded?) or there are weird constraints (can't think of any).
If the company is writing embedded systems, then purchasing a library will probably make more sense than rolling up a complete linked list implementation. If there are weird constraints to the point where reversing the linked list justifies writing special code, then any assumption that the linked list is the right data structure is worthy of doubt.

The truth is that in a few hours, looking at some of the code base and discussing it is going to give both parties a better feel for the fit of person and job than anything else. The interviewer gets a feel for how the candidate will deal with the mess that is someone else's code and build process and the candidate gets a better feel for the mess they will be dealing with.

How about if you are implementing a library for a new programming language?

Don't PL developers need to implement things like List.reverse() in their new languages all the time?

Rhetorical question, because I know the answer is yes.

Here's an example for Julia: https://github.com/JuliaLang/DataStructures.jl/blob/master/s...

    function reverse{T}(l::LinkedList{T})
        l2 = nil(T)
        for h in l
            l2 = cons(h, l2)
        end
        l2
    end
(Note that this algorithm is not an in-place reversal, so it wouldn't have worked for the OP's interview.)
Can you imagine someone hiring for such a position using such poor pre-interview screening that reversing a list made sense as an interview?

The premise of your remark indicates the absurdity of the claim of relevance. An interview for a position responsible for writing a new language in a language sufficiently low level to require a new `list.reverse` would invariably require implementation of a list type as well...what language includes <List> without a reverse?