9

You have two identical eggs. Standing in front of a 100 floor building, you wonder what is the maximum number of floors from which the egg can be dropped without breaking it. What is the minimum number of tries needed to find out the solution?

Jaguar
  • 485
  • 1
    @Dennis: You can have more than two tries: an egg doesn’t necessarily break when you drop it. – Brian M. Scott Oct 22 '12 at 12:28
  • @Brian M. Scott: right :-) – Dennis Gulko Oct 22 '12 at 12:29
  • 3
    @Dennis: Of course, we probably are to assume that a successful drop doesn’t weaken the egg, which seems rather unlikely! – Brian M. Scott Oct 22 '12 at 12:30
  • Spoiler/hint/my-laziness-in-writing-out-a-full-solution/opportunity-for-someone-else-to-write-an-answer: The answer is $O(\sqrt{N})$. – ShreevatsaR Oct 22 '12 at 12:39
  • I'm not sure wether this is a minimal solution. But, inspired by the statement of @ShreevatsaR I would do the following. Let the first egg drop from the $\sqrt{N}$-th floor of the building. If it breaks then you have to try only $\sqrt{N}$ possibilities with the other egg (starting from the first floor of course!). If it doesn't explode let the first egg drop from the $2\sqrt{N}$-th floor, and so on.. In this way in the worst case you have to do only $2\sqrt{N}$ different drops. Did I get your idea? – Giovanni De Gaetano Oct 22 '12 at 12:59
  • 1
    @GiovanniDeGaetano: You've proven that the minimum number of tries is $\leq 20$. In fact, if you go through your argument carefully, I think you actually get an upper bound of $19$. [Worst case: try floors $10, 20, \dots, 90, 91, 92, \dots, 99, 100$. I think you have to try floor $100$ in case it lands unbroken from floor $99$ since you aren't told that you know it will definitely break if dropped from floor $100$.] But you might be able to get a bit better by testing $\sqrt{M}$ floors above your previous one, where $M$ is the number of remaining steps. – Michael Joyce Oct 22 '12 at 13:06
  • If you have 1 egg and $n$ floors, you have to start with a drop from the first floor, then the second, and then up to the $n$th floor. Otherwise, if you ever skip a floor, it is possible that the egg will crack upon dropping and you will not be able to tell what floor it breaks.

    Let $f(n)$ be the smallest number of tries to guarantee finding the answer with $n$ floors and 2 eggs. Now, note that if you drop the egg from the $k$th floor, you have two possibilities:

    – only Oct 22 '12 at 13:15
  • 1
  • The egg breaks, and you have one egg to test for the remaining $k-1$ floors. By the first paragraph, this implies you will need $k$ moves in total.
  • The egg doesn't break; in this case, the first $k$ floors don't matter, and you are playing with 2 eggs and $100-k$ floors, and thus you need $1+f(100-k)$ moves in this case.
  • So this should let you code a solution to the problem.

    – only Oct 22 '12 at 13:16
  • @MichaelJoyce, Thank you! I've got your point. I was just trying to answer the case of the building with $N$ floors. – Giovanni De Gaetano Oct 22 '12 at 13:17
  • 3
    The answer is $0$: If you drop an egg onto the pavement below from a first-floor (or higher) window, then it will break. – John Bentin Oct 22 '12 at 13:29
  • 1
    @JohnBentin: The building may be in Venice, so we may not assume that the surface below is pavement. – Michael Joyce Oct 22 '12 at 14:29
  • @BrianM.Scott & all: found the answer – Jaguar Oct 23 '12 at 10:13
  • @Michael Joyce: There are no 100-storey skyscrapers in Venice. – John Bentin Oct 23 '12 at 17:18
  • This is problem from Microsoft's interview for programmers. – Norbert Oct 24 '12 at 10:20