Charles Babbage asked Ada Lovelace to invert a binary tree before he agreed to let her work on his Analytical Engine.
In his famous resume, Leonardo claimed knowledge of "efficient methods of turning a bifurcated assembly of facts upon itself, and other procedures useful in the arts of arranging and displaying facts."
It's tradition.
https://en.m.wikipedia.org/wiki/Ada_Lovelace
https://duckduckgo.com/?q=resume+of+leonardo+da+vinci&t=fpas...
TBH asking that kind of question at interview is a big red flag for me, means that they don't understand how to interview tech people and therefore probably don't understand how to manage tech people.
Just be pragmatic about it. The same way you've learned a lot of crap at school for nothing, do you expect that in the job market it would be any different?
The world is fucked up, the earlier you internalise this in yourself, the less you suffer.
- implement my own binary tree
- implement my own "invert a binary tree" algorithm
Reasons:
- my own implementation would be way worse than any other open source implementation (which has been reviewed extensively) in terms of: performance, number of bugs, correctness
- it would be a hell to maintain for future developers that have to maintain my code
- it just makes sense. Well understood algorithms do not require re-implementation
Companies, like customers, have requirements for their needs. If a company needs a potential employee to know how to invert a binary tree then it's in their right to find one.
In Google's case, as you're probably alluding to[0], although I might be mistaken, they've always had the reputation for seeking the best qualified people[1] for their work. I mean look at who's working for them - there's a lot of well-known people in their respective fields that are working for them.
Moreover, I think that the point of these tests is to help assess a potential employee's abstract thinking. And, probably, to understand one's thought process behind the solution - if there's one - to the given problem. (Which, by the way, might be helpful if you're trying to solve AI - something that's right up in Google's alley ;).
Despite the backlash over this, I believe companies will keep doing these types of tests for a long, long time. I don't think this is going away that easily. And I also think that, in the future, these tests might be done in a way you won't even know you're being tested for.
[0] - https://twitter.com/mxcl/status/608682016205344768
[1] - I understand that "best qualified" here might mean different things for different people, but you should take it literally, that is, people who are above the average in terms of abstract thinking. I suggest you read How Google Works[2], they've coined the term Smart Creative which is kind of(?) related to this.
If you ever have to maintain a code base that happens to invert binary trees, find a way to nuke that code. Whatever needs the binary tree backwards should just process the original order backwards. Then you can say you once needed to remove a silly binary tree inversion at work.
Even reversing arrays and linked lists is suspicious.
Lisp code sometimes does a nreverse pass due to pushing items onto a stack while processing, which reverses their order.
I've never had to reverse an array in C or what have you.
I recall that the block sort algorithm does some clever things with multiple reverses of subranges of an array.
So flipping the left and right, no I have never had to do that. I have had to write algorithms that processed the tree in infix, prefix and postfix order. And I have had to do it backwards, which is kind of the same but I never changed the tree for that.
As far as an interview question it is testing 2 things, 1) do you even know what a binary tree is, 2) are you comfortable with recursion. And in my case 3) are you willing to admit you do not understand and need an explanation.
From an interview perspective, all 3 are viable questions.
But I also never had the need for its application, maybe I had and missed it :)
1> (defun invert (tree)
(if (atom tree)
tree
(cons (invert (cdr tree)) (invert (car tree)))))
invert
2> (invert '(defun invert (tree)
(if (atom tree)
tree
(cons (invert (cdr tree)) (invert (car tree))))))
((((nil (((nil ((nil (nil (nil . tree) . car) . invert) (nil (nil . tree) . cdr) . invert) . cons) . tree)
(nil . tree) . atom) . if)
nil . tree) . invert) . defun)
A Lisp tree structure can be regarded not as a binary tree, but as an n-ary tree; under which we can come up with a different semantics for inverting: 3> (defun invert2 (tree)
(if (listp tree)
[mapcar invert2 (reverse tree)]
tree))
invert2
Now for comparison, we apply that to the original invert: 4> (invert2 '(defun invert (tree)
(if (atom tree)
tree
(cons (invert (cdr tree)) (invert (car tree))))))
(((((tree car) invert) ((tree cdr) invert) cons) tree (tree atom)
if)
(tree) invert defun)
That's actually halfway readable.Okay, now let's crank up the stupid knob to about 7.5.
5> (defun invert3 (tree)
(cond ((listp tree)
[mapcar invert3 (reverse tree)])
((symbolp tree) (intern (reverse (symbol-name tree))))
(t tree)))
invert3
6> (invert3 '(defun invert (tree)
(if (atom tree)
tree
(cons (invert (cdr tree)) (invert (car tree))))))
(((((eert rac) trevni) ((eert rdc) trevni) snoc) eert (eert mota)
fi)
(eert) trevni nufed)
and finally to 11: 12> (defun invert4 (tree)
(let ((tree-picture (with-out-strlist-stream (s)
(print tree s))))
(each ((line tree-picture))
(let* ((rl (reverse line))
(sb (mapcar (relate "()" ")(") rl)))
(put-line `@{sb -78}`)))))
invert4
13> (invert4 '(defun invert (tree)
(if (atom tree)
tree
(cons (invert (cdr tree)) (invert (car tree))))))
trevni nufed)
(eert mota) fi) (eert)
((eert rdc) trevni) snoc) eert
(((((eert rac) trevni)
nil
Look, text-wise left-right mirror image of the printed version of the tree!