Read · 5 min
Problem
You have two eggs, and you're in a story building. You want to determine with certainty the lowest floor from which you can drop an egg without it breaking. You can perform the following egg drop experiment: go to a floor, drop an egg, and see if it breaks. If it doesn't break, you can re-use that egg for future experiments. What's the minimum number of egg drops () you need to determine this floor? Note: that means no matter what floor it is, your algorithm must be able to compute it in steps.
Think about the problem for a little while; when you think you have an answer, click on the button below!
Discussion
If the problem were just one egg, the only thing you could do is drop it from floor , then floor , all the way till floor . Thus the minimum is egg drops in this strategy.
If the problem were many many eggs (for example ), then we could use binary search, i.e., drop one egg at floor , if it breaks drop another egg at floor else drop an egg at floor . The minimum would be egg drops in this strategy.
But alas, we only have two eggs. One idea inspired by binary search is drop the first egg at floor , then use the second egg to go through the rest of the floors. The minimum would be egg drops in this strategy.
But, we want to use our resources as efficiently as possible. In the above strategy, what if our egg survives? Then we could drop it and then the minimum would be ( egg drops if it does break, if it doesn't).
In fact, our strategy is going to be taking one egg and dropping it at some increasing sequence of floors till it breaks, then if it breaks go sequentially through the remaining floors with the second egg. For example, drop the first egg from floors , , , ..., until it breaks (if it doesn't break, then we solved it in drops). Let's say it breaks on floor . Then we have to test the second egg from floors , , ..., until at most floor . The worst case performance in this strategy is if the actual floor is , which means we test egg one times and then egg two times, so .
Perhaps we can be more efficient about this strategy. It seems that the reason it performs poorly is because for the second egg, we will always have to test cases in the worst case. Thus this algorithm is particularly better when the floor is actually on a lower floor. What if there was some way to make each case occur with an approximately equal number of egg drops?
Let's say we initially drop egg one from floor . Then at worst case, we will have egg drops from the second egg, so drops in total. If egg one survives from floor , we want to drop it from floor . We will have to test from to ( drops) for drops in total. In order to make all the cases approximately equal, we want . In fact, this floor number is intuitive, as we need to account for the one egg drop that we already performed by reducing the number of drops for the second egg by one, which means we want to go up not floors, but floors. For the third choice in floors, we will have already made two drops, so we need to go up . Thus the strategy is to drop it at floors , , , ...; with this strategy, each case will always use exactly egg drops. Finally, since we want to stop dropping eggs one once we get to the th floor, we want the last drop to be . Hence So this strategy gives drops.
In general, for floors, this method solves the problem in . Fun fact: even the above strategy of dropping egg one at floors , , ... gives . However, this algorithm gives an absolute minimum number of drops.
Information Theory
It's very hard to prove that this algorithm gives the absolute minimum number of steps. Most information theory proofs involve formalizing the concept of a strategy and showing that for any other strategy, there is some case where it does at least as bad as your strategy. I'll conclude with the way I would prove this optimality (and I'd like to see people post if they have better ways to prove this).
Proof. Let be an optimal strategy to solve this problem. In particular, takes two eggs, chooses one arbitrarily to be egg one () and the other to be egg two (), then assigns a sequence of drops for and . Since the eggs both function equally and are indistinguishable, we can transform into a strategy where we perform a sequence of drops on until it breaks, then on .
Let's say drops from floors , stopping when it breaks. We can assume since no new information is gained by dropping it from a floor less than one we've already seen. Let be floor for notational ease. Suppose breaks on floor . Then we will necessarily have to test floors , since it is possible that the target floor is any of these floors. Therefore, in total we will need drops for and drops for , a total of .
Let . Now we have to take the worst case of all of these, i.e., we need to compute
where for all and ( in this case). Note: we deduced that sum from the fact that is necessarily true. Finally, by the averaging principle, we must have
By AM-GM, we have
so any strategy will take at least that many steps. When , that means any strategy needs egg drops, so the minimum is . It thus suffices to check that is achievable, but we have already done this in the previous section, so we are done.