Read · 5 min

Problem

You have two eggs, and you're in a 100100 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 (NN) you need to determine this floor? Note: that means no matter what floor it is, your algorithm must be able to compute it in ≤N\le N 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 11, then floor 22, all the way till floor 100100. Thus the minimum is 100100 egg drops in this strategy.

If the problem were many many eggs (for example 100100), then we could use binary search, i.e., drop one egg at floor 5050, if it breaks drop another egg at floor 2525 else drop an egg at floor 7575. The minimum would be 77 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 5050, then use the second egg to go through the rest of the floors. The minimum would be 5151 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 7575 and then the minimum would be 5050 (5050 egg drops if it does break, 2727 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 1010, 2020, 3030, ..., 100100 until it breaks (if it doesn't break, then we solved it in 1010 drops). Let's say it breaks on floor 3030. Then we have to test the second egg from floors 2121, 2222, ..., until at most floor 2929. The worst case performance in this strategy is if the actual floor is 9999, which means we test egg one 1010 times and then egg two 99 times, so 1919.

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 99 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 xx. Then at worst case, we will have x−1x-1 egg drops from the second egg, so 1+(x−1)=x1+(x-1)=x drops in total. If egg one survives from floor xx, we want to drop it from floor yy. We will have to test from x+1x+1 to y−1y-1 (y−x−1y-x-1 drops) for y−x+1y-x+1 drops in total. In order to make all the cases approximately equal, we want x=y−x+1  ⟹  y=2x−1x=y-x+1\implies y=2x-1. 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 xx floors, but x−1x-1 floors. For the third choice in floors, we will have already made two drops, so we need to go up x−2x-2. Thus the strategy is to drop it at floors xx, x+(x−1)x+(x-1), x+(x−1)+(x−2)x+(x-1)+(x-2), ...; with this strategy, each case will always use exactly xx egg drops. Finally, since we want to stop dropping eggs one once we get to the 100100th floor, we want the last drop to be ≥100\ge100. Hence x+(x−1)+(x−2)+⋯+1=x(x+1)2≥100  ⟹  x≥14.x+(x-1)+(x-2)+\dots+1=\frac{x(x+1)}2\ge100\implies x\ge14. So this strategy gives 1414 drops.

In general, for nn floors, this method solves the problem in O(n)O(\sqrt n). Fun fact: even the above strategy of dropping egg one at floors 1010, 2020, ... gives O(n)O(\sqrt n). 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 SS be an optimal strategy to solve this problem. In particular, SS takes two eggs, chooses one arbitrarily to be egg one (E1E_1) and the other to be egg two (E2E_2), then assigns a sequence of drops for E1E_1 and E2E_2. Since the eggs both function equally and are indistinguishable, we can transform SS into a strategy where we perform a sequence of drops on E1E_1 until it breaks, then on E2E_2.

Let's say SS drops E1E_1 from floors a1,a2,…,ama_1,a_2,\dots,a_m, stopping when it breaks. We can assume a1<a2<…a_1<a_2<\dots since no new information is gained by dropping it from a floor less than one we've already seen. Let a0a_0 be floor 00 for notational ease. Suppose E1E_1 breaks on floor aka_k. Then we will necessarily have to test floors ak−1+1,…,ak−1a_{k-1}+1,\dots,a_k-1, since it is possible that the target floor is any of these floors. Therefore, in total we will need ak−ak−1−1a_k-a_{k-1}-1 drops for E2E_2 and kk drops for E1E_1, a total of (k−1)+ak−ak−1(k-1)+a_k-a_{k-1}.

Let Δk=ak−ak−1\Delta_k=a_k-a_{k-1}. Now we have to take the worst case of all of these, i.e., we need to compute

max⁡{Δ1,1+Δ2,2+Δ3,… }\max\left\{\Delta_1,1+\Delta_2,2+\Delta_3,\dots\right\}

where Δi>0\Delta_i>0 for all i≥1i\ge1 and Δ1+Δ2+⋯=n\Delta_1+\Delta_2+\dots=n (n=100n=100 in this case). Note: we deduced that sum from the fact that ak=na_k=n is necessarily true. Finally, by the averaging principle, we must have

max⁡{Δ1,1+Δ2,2+Δ3,… }≥Δ1+(1+Δ2)+⋯+(m−1+Δm)m=(1+2+⋯+m−1)+(Δ1+Δ2+⋯+Δm)m=m−12+nm.\begin{align*} \max\{\Delta_1,1+\Delta_2,2+\Delta_3,\dots\}&\ge \frac{\Delta_1+(1+\Delta_2)+\dots+(m-1+\Delta_m)}m\\&= \frac{(1+2+\dots+m-1)+(\Delta_1+\Delta_2+\dots+\Delta_m)}m\\&= \frac{m-1}2+\frac{n}m.& \end{align*}

By AM-GM, we have

m−12+nm=−12+(m2+nm)≥−12+2n2\frac{m-1}2+\frac{n}m=-\frac12+\left(\frac{m}2+\frac{n}m\right)\ge-\frac12+2\sqrt{\frac{n}2}

so any strategy will take at least that many steps. When n=100n=100, that means any strategy needs −12+2100/2≈13.6-\frac12+2\sqrt{100/2}\approx13.6 egg drops, so the minimum is ⌈13.6⌉=14\lceil13.6\rceil=14. It thus suffices to check that 1414 is achievable, but we have already done this in the previous section, so we are done. ■\blacksquare

Type a concept, like “slope” or “photosynthesis”.

↑ ↓ to choose · Enter to openSearch explanations and problems