Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I will still never understand why I was asked to code an implementation of a binary tree for an interview once, something that is usually left to those who implement standard libraries or similar low-level needs, for a job that was basically writing user interfaces.


I asked this once as a take home for an interview. First, I copied the first hit for “implement binary tree in C” from google. I made sure it compiled and then deleted the implementation of the insert method. That was the take home. I wanted to know that the applicant could reimplement this one missing function and compile the project and not get immediately stuck.


Because an implementation of a binary tree is dead simple.


I 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.


People 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.


> 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?

It's easy to make an imbalanced binary tree.

I once decided that building a binary tree based only on a friend's explanation of how they worked; it seemed like it would be a good learning exercise for myself (I don't have a CS degree). It was like 10 minutes of coding to get it minimally working & tested, but then I had to work out all of the different rotations to make it a balanced binary tree (I don't know that an imbalanced binary tree is ever actually useful, as it could just work out to be a slower linked-list in the worst case scenario).

I did it once, took me a little while, but I don't know that I could do it again without having to reexamine all of the different cases that result in imbalance and the rotations that respond to each.


Binary search tree is more difficult than a binary tree. A BST is a BT but a BT is not a BST. So making a normal BT is just pointing nodes.


Huh... TIL, as they say.


There’s more than making a node the points to other nodes. That’s not what makes a binary tree a binary tree.


I mean… it literally is all there is to it


Well, 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.


Again, 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.


It isnt the knowledge itself, it is making sure it works and is tested, etc, all in <45m. It serves as a worse fizzbuzz imo.


Do you want me to show you a single-linked list or a double-linked one?

    [data, *next] [data, *next] [data, *next] [data, *next] 
If next is null (0), it's the end of the list.


This 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.


Not to those who struggle with the notion of a pointer (or other such “fundamentals”), it’s not.


There 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.


The 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.


Why would it be balanced. Nobody said it was a search tree.


Just 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;


If 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.


You mean a naive one, that is, a useless one...or a practical one? Such as, at least something like LLRB?


A 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.


To 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.


Correct 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.


It 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.


> 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.


Yes, you are of course correct. I was thinking of a heap, which is a complete binary tree.


It 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.


This 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.


Because sometimes the requirement is that the engineer being hired is able to come up with efficient algorithms. You don't have to be able to create the textbook binary tree algorithms, but you should be able to derive a reasonably performant algorithm with simple data structures if it matters to the position you're being hired for.


> requirement is that the engineer being hired is able to come up with efficient algorithms.

Coding binary tree is not the way to discover that.

Presenting a task and observing people solving it and explaining computation complexity behind their solution and the thinking behind their solution is how you do that.


It's a fine way of doing it, although certainly it shouldn't be the only algorithms question.

It's a simple data structures / algorithms question. Can you think in terms of how data is stored in memory? Do you understand the tradeoffs of storing data in one form or another? Can you work with recursive algorithms? Given a challenge of having to think about data in a form you haven't recently, can you wrap your mind around the characteristics of that data structure?

If you're getting asked all this for a ReactJS UI building job, then the interviewer is probably off-base. But otherwise, make sure you're applying for jobs you're suited for. If you don't want a job where coming up with efficient algorithms is important, that's fine - there's plenty of others out there.


> Can you think in terms of how data is stored in memory?

If you're using a modern OS, you don't know this. The kernel remaps everything and pretends to give you a linear chunk of memory. You don't know what's in the various caches and what's been paged out of ram completely.

Everything is built on layers of abstraction. C is an abstraction layer that fits poorly into what modern CPUs and operating systems actually do, but everything is programmed to cater to C's assumptions so people don't need to rewrite their programs.

Higher languages don't necessarily use the data structure you think they do and they can switch the implementation details based on the size.

"Simple data structures" are only simple because you pick one specific layer of abstraction to work with.


That's not an excuse for not being able to reason about data structures if your job requires working with data structures and algorithms. The logical extension of your argument is "put everything in a linked list and let the OS work it out."


If I give a candidate a truly real world difficult problem with an hour or even a day to solve it, some people who are not so smart will get the correct answer by luck, some people who are brilliant will start off with a bad approach and not get there.

If I ask if you can do something basic binary tree things, I will also not know if you are brilliant, but I can at least rule out if you are totally clueless or not.


Sorry, but regurgitating binary tree operations is not "coming up with efficient algorithms".


Well hopefully there's some layering in the question if it matters to the job. If you have an engineering position in which a high degree of CS competence is required and the candidate can't even make it past the initial layer of "regurgitating binary tree operations" then you have the wrong candidate.


> If you have an engineering position in which a high degree of CS competence is required and the candidate can't even make it past the initial layer of "regurgitating binary tree operations" then you have the wrong candidate.

Or you have a candidate who understands binary trees, can implement it but can't regurgitate binary tree operations from the top of their memory.


For me the point would be that a no-frills binary tree is simple enough that any dev qualified for such a position should be able to figure out what needs to be done from the specification, without needing to regurgitate anything.

If they can't recall how balancing is done that would be fine with me, as long as they mention it.


Exactly. And that's the reason you'd probably layer the question with some more complex asks after the initial answer. If somebody has memorized the algorithm but then can't explain further, it's obvious they've "regurgitated" without really having a deep understanding of the problem being solved with the data structure. By contrast, a good candidate may not be able to immediately repeat the algorithm, but can reason their way there pretty quickly.


> By contrast, a good candidate may not be able to immediately repeat the algorithm, but can reason their way there pretty quickly.

I know dozens of good candidates who can't get there pretty quickly but are really good. I also know dozens of good candidates who can get there pretty quickly but won't be good enough on the job.


Maybe they'd be really good at other engineer positions, but if they can't get to a binary tree algorithm reasonably fast I wouldn't hire them for backend positions that require CS reasoning.


> Maybe they'd be really good at other engineer positions, but if they can't get to a binary tree algorithm reasonably fast I wouldn't hire them for backend positions that require CS reasoning.

You not hiring for backend positions doesn't mean they are not good at the positions. It just means they aren't good at your signals.


For me it's a strong signal, but it shouldn't be the only signal. And I do think it's good to have a diverse team.

Some of my coworkers are not nearly as strong as me when it comes to "CS details" like data structures or programming languages, but they have other attributes which complement me and which are good for the team.

Having a bunch of me's (or any of the others) would be a disaster.


You mean, "regurgitate" operations like this?

    find = lambda k, t: t if t is None or k == t.k else find(k, t.left) if k < t.k else find(k, t.right)
I mean, figuring out how to rebalance a red-black tree after insertion is more demanding, and you could legitimately quibble with my golfing here, but I feel like the reasoning required to write that function (given the tree definition) is just not that advanced. It's like the FizzBuzz of pointers.


That was probably a question to determine basic competency in coding, simple data structures and simple algorithms. I.e. if he can do this, that means he's at least minimally competent. if he can't, then we don't have to waste any more time.


Because it is the 2nd easiest possible data structure to implement, if you can't do that it means you don't grasp how data can point to other data and how you can leverage recursion to make hard problems easy to solve. Perhaps they were hiring for someone that they needed to be capable of more than just trivial CRUD wiring. Even when you aren't writing core libraries these things come up constantly, sometimes at higher levels when designing systems and how they interact even.

I implore you get to a state where this isn't hard for you, it will make you capable of more.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: