"Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off."
"Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off."
Every time I have to use it, I have time to ponder about my life choices (like why am I using a mac to do sysadmin/dev work?).
https://stackoverflow.com/questions/28099683/algorithm-for-d...
My first thought was "flatten the tree depth first and then reverse that linear list" which I knew to be inefficient, but I'm guessing I'd eventually stumble upon the recursive version of that when I got down to the nitty-gritty.
But now that I think about it can't you solve this problem with a single boolean flag that says "read right to left" or "read left to right"? I.e,. if the children are labeled `A` and `B` and you interpret "normal" as A,B and "inverted" as "B,A", isn't that sufficient? That's basically what the recursive version of this algorithm is doing, but why bother to actually move data around?
I get that this is an abstract problem, but I'm having trouble understanding any practical use case for this concept.
If you've got a real, in-memory, linked-list-style binary tree my `is_inverted` flag seems like it would be sufficient.
And if the real problem is moving blocks of data around on disk or in buffers, then isn't this all a question of the exact "flat" representation of this tree?
If my whiteboard answer was "keep a flag that says whether or not the tree is inverted" would I be hired?
Can someone reframe this question in a way that makes it clear that the trivial rtl-mode vs ltr-mode is insufficient? The only way this question makes sense to me is if you're really looking for someone to come up with the serialized representation of that data structure that makes this transform efficient, but none of the public answers to this question seem to cover that at all.
I honestly don't understand how this problem is meaningful for the in-memory, linked-list case.
(Out of principle, I prefer to ask interview questions that are based on real problems I've had to solve in my job, but sometimes they're harder to ask coherently than well-defined/concise problems like "invert a binary tree" so I can see the appeal)
> If my whiteboard answer was "keep a flag that says whether or not the tree is inverted" would I be hired?
Personally I'd give you bonus points for recognizing that could be a solution, then clarify that I wanted a solution that actually moved the data around and give you the opportunity to answer it in light of that clarification.