This is another question I saw around the place somewhere, attributed to Google. It goes along the following lines…

You are given two marbles, and told that they will break when dropped from some certain height (and presumably suffer no damage if dropped from below that height). You’re then taken to a 100 story building (presumably higher than the certain height), and asked to find the highest floor your can drop a marble from without breaking it as efficiently as possible.

Effectively, what we have here is a search problem, but we’re limited to making at most two checks above the upper limit. My first though was a binary search but clearly there’s no way to limit the number of checks above the value, so we can’t guarantee we will actually find the correct value.

As far as I can see, the best policy is to use the first marble in a broad first pass to narrow down the possibilities, then use the second to actually find the correct floor within a limited range.

Let’s say we start at the ground, and move up x floors at a time dropping the first marble until we find the first floor at which it breaks (let’s say it’s fu). We then need to use the second marble to search from fu to fu - x (let’s call it fl).

We already know that the marble is safe at fl, So we’ve got to check fl + 1, then fl + 2 and so on until we reach fu - 1 (aka fl + x - 1). On average, we’ll need to perform (x - 1) / 2 checks to find the correct floor with the second marble.

The key question, then, is what value should be used for x. Obviously the value of this is going to vary with the height of the building, which we were told was 100 floors, but to keep things generic, let’s call it h. On average, we’ll need to perform (1/2 * h) / x checks with the first marble, so in total on average we’ll need to perform the following number of checks.

((1/2 * h) / x) + ((x - 1) / 2)
(forgive me for not marking up the equations properly)

or simplifying,

(h / 2x) + (x / 2) - (1 / 2)

What we need is to find the value for x where this expression is a minimum. To get an idea of what we’re looking at, we could substitute in h = 100 and graph this, which gives us the following…

A graph of y = (100 / 2x) + (x / 2) - (1 / 2)

From a glance, it looks very much like we want x = 10, but just for the sake of checking, we can take the derivative and find the zeros (local minima and maxima are where the slope is zero)…

y = (h / 2x) + (x / 2) - (1 / 2)

dy/dx = (-(h / 2) / x^2) + 1 / 2

and since we’re looking for the zeros…

0 = (-(h / 2) / x^2) + 1 / 2

1 / 2 = (h / 2) * x^-2

x^-2 = 1 / h

x^2 = h

x = sqrt(h)

(If we hadn’t already drawn a graph, we’d need to check that this was a in fact a local minima, and also confirm it’s below x = 0 and x = 100, but I’ll skip it for now).

So, for out case of a 100 story building, we should start by going up ten floors at a time until the marble breaks, then walk through the previous nine floors one at a time with the other marble.

I’ve got to say though, that’s a lot more maths than I’ve ever needed in an interview. I’ve got to say it’s quite a cute question though, and requires a decent amount of thought.

Technorati Tags: , , ,