8 ms·
Because an implementation of a binary tree is dead simple.
by nostrebored 5y ago
Because an implementation of a binary tree is dead simple.
- jhgb 5y agoYou mean a naive one, that is, a useless one...or a practical one? Such as, at least something like LLRB?
- ascar 5y agoA binary tree is just a tree data structure where each node has at most two children. And that's dead simple and every programmer should be able to implement that when aksed. Note that it doesn't even have the search (i.e. sorted) requirement and much less the self-balancing one. You can work your way towards that with the right questions and that would be an acceptable way to structure an interview. Start with something simple everyone should be able to do and then work your way up the requirements and discuss potential problems/pitfalls and their solutions. Nobody talked about implementing a specific variant of a red-black tree when the general topic of self-balancing binary search tree hasn't even be mentioned.
- jhgb 5y agoIt is dead simple. What you're describing (two children, no constraints) is basically a cons cell. Yet I suspect that most people don't use cons cells as binary trees. Which is probably why I didn't understand that apparently rather nebulous topic as being this generic.
- sureglymop 5y agoTo add to that, a binary tree is often not really "implemented" but simply encoded in an array. That is, the children of any given node i are 2i and 2i+1 in the array. So depending on the actual exercise/interview question you might able to just show that, especially of you have to use pseudo code.
- jhgb 5y agoCorrect me if I'm wrong, but isn't this exactly the case where you don't want an unbalanced tree? A degenerate tree is bad enough; a degenerate tree implemented as an array in this way is catastrophic. So it seems to me that if you're refining the topic further in some sort of talk, the topic of balancing should precede the topic of physically representing the tree as an array.
- ascar 5y agoIt all depends on the specific topic. If I just want to store my data in binary tree structure without the possibility of deletes and just with an insertion-order requirement, then the tree is automatically balanced and using an array like that is totally fine. The use-cases are probably rare (I can't think of a situation from the top of my head), but binary tree doesn't have to mean search tree. The self-balancing part is usually added after the search part as balance is trivial/unimportant when the order/position of the elements has no constraints.
- jhgb 5y ago> The use-cases are probably rare (I can't think of a situation from the top of my head) I imagine there's quite a lot of them. Things like VP-trees and such get often built statically and could be implemented in a similar way. Not sure if the simple (2i, 2i+1) schema is optimal for this but perhaps it could be adapted.
- sureglymop 5y agoYes, you are of course correct. I was thinking of a heap, which is a complete binary tree.
- magicalhippo 5y agoThis is another point in which this can be a good interviewing question. For certain jobs you don't want someone who just forges ahead with an unclear specification. That said, for most dev jobs implementing a binary tree isn't necessary knowledge. But if I need a dev who can get their hands dirty I would be weary of hiring someone who can't take a decent stab at it during an interview.
- Koshkin 5y agoNot to those who struggle with the notion of a pointer (or other such “fundamentals”), it’s not.
- jb1991 5y agoThis is an arrogant comment. You are right that the general algorithm of a binary tree is not complicated. But coding it under pressure, for anyone who is not writing that kind of code on a daily basis, which is most people, and especially for a job where you’re not required to write that kind of code, is ridiculous. If you audition for the New York City ballet, they don’t expect you to ice skate.
- kayodelycaon 5y agoI have a computer science degree from 10 years ago and I couldn't tell you how to implement one. I vaguely know the general principles but I'd have to sit down and start from scratch on how to implement one. Why? The only time I knowingly used them is with database indices. For everything else I do, they are an unnecessary implementation detail. It doesn't matter what Ruby's Set uses.
- gameswithgo 5y agoPeople who make this comment, I generally don't believe you, or don't believe you have done any programming. If I put a gun to your head and give you an hour, you can't make a node that points to two other nodes of the same type? If you really can't, you should get familiar with how data can point to other data, it is fundamental and done all the time.
- jb1991 5y agoThere’s more than making a node the points to other nodes. That’s not what makes a binary tree a binary tree.
- nostrebored 5y agoI mean… it literally is all there is to it
- jb1991 5y agoWell, the requirements I faced were to implement the tree such that nodes were inserted with the standard ordering you would expect. Walking the tree to find the best place to insert them, etc.
- maximus-decimus 5y agoAgain, that's stupidly easy. Right is bigger, left is smaller. If the current node is a leaf, insert left if smaller and right if bigger. If not a leaf nods, run the method recursively on the left it it's smaller, on the right if it's bigger. If they asked you to write a self-balancing binary tree it's one thing, but there's nothing tricky about writing a normal ordered binary tree.
- ClumsyPilot 5y agoThere are loads of simple tasks, there should be a bit more reasonaing as to why this is a good filter relevant to the job's day to day. For example I think writing some logic to filter datapoints or timespans is much better and more practical. But it takes more effort than just opening and pocking binary tree.
- marcosdumay 5y agoThe fact that you didn't even think about asking if it's balanced or not, and just went into declaring stupid everyone that considers the problem hard is annoying. I wouldn't want to have an interview with you, or you selecting people for a team I'm on.
- CheezeIt 5y agoWhy would it be balanced. Nobody said it was a search tree.
- marcosdumay 5y agoJust the fact that somebody is talking about "implementing" it implies there's further complication than defining a recursive type. People in general do not communicate on the cleanest ambiguosity-free way;
- nostrebored 5y agoIf you can’t remember the details or understand what makes the problem hard I doubt it would’ve been fine even given a whole day. I’ve interviewed a _lot_ of people. The people who gripe the loudest about algorithms haven’t had a chance just from answers to situational questions.