Page 1 of 1
software interview question
Posted: Wed Feb 27, 2008 7:18 pm
by Jonathan
How do you calculate the height of a tree?
Answer first, then Google.
Re: software interview question
Posted: Wed Feb 27, 2008 8:54 pm
by Peijen
Dwindlehop wrote:How do you calculate the height of a tree?
Answer first, then Google.
From where?
Posted: Wed Feb 27, 2008 10:03 pm
by Jonathan
Not sure I understand what you mean. It' s a question we use to screen potential software developers for in-house tools. Just don't google the answer, is all I'm saying.
Posted: Wed Feb 27, 2008 10:25 pm
by Peijen
Ok, fine let me ask it differently.
Daytime or nighttime?
Posted: Wed Feb 27, 2008 10:33 pm
by Jonathan
Heh.
Let's stipulate it's a binary tree.

Posted: Wed Feb 27, 2008 10:41 pm
by Peijen
Oh, that kind of tree. rand(log_2(size_of_tree), size_of_tree)
How accurate does it have to be?
Posted: Wed Feb 27, 2008 10:54 pm
by Jonathan
I assume the bounds and exact answer for a balanced tree would suffice.
Posted: Wed Feb 27, 2008 11:23 pm
by quantus
What info/functions about the tree/node are given (what language? C++/STL)? Dumb simple way is to recurse to the bottom of the tree and just keep passing up the largest depth encountered from the children.
Posted: Wed Feb 27, 2008 11:28 pm
by Jonathan
It's a pseudocode implementation of your own devising.
Posted: Thu Feb 28, 2008 12:19 am
by quantus
heh, then I track node depth for each node and update max_depth on inserts when required.
That's an ok solution if you need to know the depth all the time. Really, you need to give a profile of the types of operations that will be performed on the tree so that the right kind of tree/data structure is used to optimize perf. If this is just gonna be used on small scale problems, then it doesn't really matter what you do as long as it works. If you're gonna have a billion entries, then you need to put real thought into this. The Google interview was interesting because they gave a fairly concrete real world'ish problem describing the amount of entries, how much memory you have, etc...
Posted: Sat Mar 01, 2008 12:36 am
by George
I thought the same thing as Peijen.
I need a protractor, straw, tape, fishing line, a weight, a calculator, and a ruler or measuring tape. With a calculus textbook to refresh my memory and a lot of time, I could do it without the calculator, using series to compute the trig functions.
If I had a saw, then I can skip everything except the ruler. That raises the question of whether you want the height of a tree at the time you ask or at the time I answer? With a flamethrower or explosives, I can hard-code the answer as zero with some side effects.
Alternatively a monkey and string would work, if the monkey could be convinced to climb straight up. A rocket or other projectile could be substituted for the monkey if the branches are spare enough and close enough to the top.
A laser rangefinder plus calculator would work.
You could have a team of cheerleaders of known heights form a pyramid. Unfortunately, that algorithm doesn't scale very well.
Do the roots count, or only the portion above ground?
Coincidentally, almost this exact thing came up at work recently. I had a quad-tree and needed to know the number of things stored in or below a node. So, I just put in the recursive solution to traverse each node and keep a count. Turns out that's pretty expensive, at least relative to the other parts of that code path. So, maintaining the count in each node on inserts and removes is likely to be the better solution (I haven't timed the change yet) in that one case.
So, with that recent experience, I'd say for a general tree, I'd probably consider maintaining the height in each node on updates and removes. It Like Joe says, though, it would depend on the relative frequency and cost of the operations and on how much memory you wanted to throw at the problem.
But, IIRC, a red-black tree guarantees that the maximum and minimum depth are bounded as something like log2(n)+/-1? Been a while. Maybe the number of black nodes to any leaf is constant? Anyway you could just keep a count of the inserts - removes and get an answer that is within one of the correct number. That's if you are able to use a red-black tree. There are other self-balancing trees that probably make similar guarantees.
Or, drawing from my side-effect-based solution to the plant tree version, you could make a one-tree and know that the height = N.
Ok, now off to google.
Posted: Sat Mar 01, 2008 12:56 am
by quantus
George wrote:I thought the same thing as Peijen.
I need a protractor, straw, tape, fishing line, a weight, a calculator, and a ruler or measuring tape. With a calculus textbook to refresh my memory and a lot of time, I could do it without the calculator, using series to compute the trig functions.
If I had a saw, then I can skip everything except the ruler. That raises the question of whether you want the height of a tree at the time you ask or at the time I answer? With a flamethrower or explosives, I can hard-code the answer as zero with some side effects.
Alternatively a monkey and string would work, if the monkey could be convinced to climb straight up. A rocket or other projectile could be substituted for the monkey if the branches are spare enough and close enough to the top.
A laser rangefinder plus calculator would work.
You could have a team of cheerleaders of known heights form a pyramid. Unfortunately, that algorithm doesn't scale very well.
Do the roots count, or only the portion above ground?
Ok, how do you do it using the fewest and cheapest tools available? We want a method that will provide an answer that is accurate to within 1%. Roots don't count. Height is defined from ground level to the very top at the time of the measurement. It costs $60/hr/person once the measurement method is started. Your time taken to determine a method to acquire the answer is free. I think this will make solutions such as atomizing the tree and saying 0 cost a lot and be suboptimal even though it is very fast and accurate.
Posted: Sat Mar 01, 2008 1:11 am
by George
Oops, in the course of googling for the programming problem, I came across several solutions for the plant problem. So I'd be cheating. Let's see if anyone else can come up with better or more creative solutions.
Is there a specific page you were think about with your no googling requirement, because I couldn't find anything that looked like it was directly addressing the programming problem.
Posted: Sat Mar 01, 2008 1:25 am
by Jonathan
I didn't pre-Google it, no.