A software engineer on twitter posted his experience at the US airport immigration, he was literally given an A4 paper and a pen to do this. As a CS major, Lord knows I’ll cast if I was confronted with this, but can you imagine travelling 23hrs only to get stomped by Google Interview questions by immigration officers.
If I remember correctly the easiest method to remember is to do an inorder traversal of the BST into a list, and then recursively reconstruct a subtree rooted by the midpoint of each half of the list.
A BST can be converted easily to a sorted list using inorder traversal. I guess it runs in O(N) time and O(N) memory complexity because we have to maintain a N-length list as temporary storage.
I wrote this on the fly so I guess there will be bugs here and there but I guess if you can remember the basic idea you can always rewrite and clean bugs to your interviewer or immigration guy’s satisfaction.
If the guy starts talking about Red-Black trees or AVL trees, I then give up.
Trees are my comfort zone, if you wake me up from sleep and ask me a tree question I should be able to give a manageable answer.
My final question in such an interview (6 hours) was to determine if a Generic Tree is a BST, lol, I finished the question with the best answer in less than half of the time, then had time to gist with the guy as dude didn’t have any more questions for me.
If you throw me a Graph question though, you will totally throw me off balance before (if ever) I can get myself together to answer you. Everybody has their forte I guess.
When I participated in ACM/Nigeria Universities Programming Contests back then, I came up with solutions on the spot for problems I encountered. It took me longer to answer those questions because I didn’t know those algorithms off the top of my head. Bosses like @Obaro_Ogodo finished those questions fast. You know why.
I always told myself i didn’t need that shit to build printivo.com or any other project i’ve worked on in the past. But recently, I had to go through some of these algorithms again and I have come to realize that they are so basic any programmer should understand them. Every programmer should understand the Big O notation and be able to judge the complexities of whatever they write. It shows you know what you are doing and it makes you stand out from someone that stumbles on the field.
You might never have to sort an actual binary tree or use Djikstra algorithm but the lessons influence how you write code one way or the other. Bosses @zaga & @mayojava can probably tell you more.