5 ms·
Isn't inverting a binary tree just this? (That's what some people seem to think, when I search for the problem.) 1> (defun invert (tree) (if (atom tre
by kazinator 9y ago
Isn't inverting a binary tree just this? (That's what some people seem to think, when I search for the problem.)
1> (defun invert (tree)
(if (atom tree)
tree
(list (invert (cadr tree)) (invert (car tree)))))
invert
2> (invert '(((1 2) (3 4)) ((5 6) (7 8))))
(((8 7) (6 5)) ((4 3) (2 1)))
Here I modeled the binary tree using two-element lists as the nodes (thus using two cons cells where one would do) for the sake of presentational clarity.
What are the real requirements?
- pkaye 9y agoYes, I think it is just recursively swapping the left and right branches of the tree.
- rurban 9y agoI would have written the same. I have no idea what Google understands by "inverting a binary tree", but to me it looks this. "Recursively swap left with right." Now just convert that to an inferior language, like java or python, so they can understand it.
- jsolson 9y agoI mean, I suppose I'd be happier with Racket? Bonus points for building a #lang for expressing trees and operations on them. (note: this probably wouldn't actually get you bonus points, and might result in my wildly gesticulating to get your attention to try to drag you back to the actual interview)
- sumitgt 9y agoIf you are faced with a question like this in a Google interview, and you simply state the answer above right away, you are only solving part of the problem. A Google interviewer expects you to ask questions and clarify any simplifying assumptions you may have made. For eg: Are you assuming right at the beginning that the entire tree fits in memory? If so, Google interviewers expect you to clarify that at the beginning. They might later ask you to modify your solution for cases where your assumptions don't hold true. From what I know, the biggest mistake people make during tech interviews with the big 5 is directly jumping to solutions without probing deeper. At the scale at which the big 5 operate, many routinely simple assumptions can be problematic.
- kazinator 9y agoIf a tree is so big it doesn't fit into memory, what are you trying to achieve by swapping it? How about just working with the original tree the way it is and just traversing it in reverse order, if necessary. I would probably ask if this is in-place or do we treat the input tree as immutable.
- saagarjha 9y ago> I would probably ask if this is in-place or do we treat the input tree as immutable. …that's what the parent comment is implying. Ask questions and don't assume things.
- kazinator 9y agoIf you can code a prototype as fast as asking question, why not. Code it up and then ask, "how about this: what requirements are missing from this that require changes or a rewrite?" Asking too many questions sounds like you're trying to delay coding something up. Suppose you just ask questions and they pile on more the requirements. Then the combination of requirements makes it so hard that you can't really code anything on a whiteboard. You're hosed. If you incrementally prototype, at least you arrive at the same point having some iterations of code on the whiteboard.
- sumitgt 9y agoI'm sorry, this is terrible advice. Trust me, the interviewer is never gonna pile up requirements on you. In fact, they are trained to ensure that what they ask can be completed in the time allotted (with time to spare for candidate's questions). More often than not, when you ask clarifying questions, you will end up with a much different problem (and sometimes simpler) than what you expected. > Asking too many questions sounds like you're trying to delay coding something up. No it doesn't. > If you can code a prototype as fast as asking question, why not. Code it up and then ask, "how about this: what requirements are missing from this that require changes or a rewrite?" This is terrible, you are now making the interviewer do your work. You need to tell the interviewer what the boundary conditions of your solution are. That gives valuable data about your understanding of the solution space.