“Figure out the highest floor of a 100-floor building an egg can be dropped without breaking, given two eggs”
The Egg Dropping Puzzle is a mathematical puzzle that has been around the internet for some time now, which is known to be adopted in interviews of major companies like Google, Microsoft, Accenture and even Hewlett Packard.
You are to determine the minimum number of attempts required in the worst case to find the critical floor.
Photo by Pablo Vargas via Unsplash
While the answer is not too hard to understand, it takes some twists of one’s thought to arrive at the answer.
The solution is derived using the utilization of a restricted number of trials. And then the 2-egg, n-floor solution can be easily generalized.
A step-by-step solution to optimize the answer:
13 trials can test 13 * 14 / 2 = 91 floors14 trials can test 14 * 15 / 2 = 105 floors
Thus it takes 14 trials to test a 100-floor building.
To derive the general solution of the egg dropping puzzle with any number of eggs, let’s review the 2-egg case again. We will interpret the solution in an alternative way.
To keep things simple, we are going to work with 15 floors only. We will drop the eggs (IF an egg does not break) on floors 5, 9, 12, 14, 15, which implies that a maximum of 5 trials is sufficient to obtain the answer. The table below shows all the possible outcomes.
Remarks:
There are in total 16 cases, corresponding to the answers 0 to 15 for the 15-story building. By analyzing the distribution of the trials that break the egg, i.e. the yellow cells, we can see that the 16 cases are indeed divided into 3 groups:
0 yellow cells: 1 case (rows in orange)
1 yellow cell: 5 cases (rows in green)
2 yellow cells: 10 cases (rows in cyan)
To utilize all the trials and the eggs, the number of cases should be the same as the number of total possible combinations of the yellow cells, i.e.
Therefore the 15-floor building can be represented by:
And we can generalize the solution for 2 eggs given m trials:
Maximum number of floors that can be tested will be…
As usual, we will start with a simple case: 3 eggs with 4 trials.
Following the same strategy, if an egg does not break on the 11th floor, we will try floor 13, and then 14 if an egg does not break on the 13th floor.
A picture is worth a thousand words (or so they say):
The combinatorial view can be applied here too! The reason is that the optimal strategy should utilize all the possible trials with the given number of eggs, where every different egg-breaking combination (a total of 15 combinations in this case) should produce a different answer.
To express this numerically, it will be:
Remarks:
Based on the above discussion, the general solution will be:
With x eggs and m trials, the maximum number of floors of a building we can test is given by:
Considering the possible case where x > m, we should write the general solution as:
A quick question for the readers: what is the implication when x > m?
An extract of the numerical values of the general solution is shown below. The answer for the initial problem with 2 eggs and a 100-floor building is highlighted, i.e. 100 is bound by 91 and 105 hence the answer is 14 trials.
A full spreadsheet with these numerical values and a number-of-trial calculator is available.
Originally published at code.oursky.com on July 18, 2016. Any comments? Share with us below!
😻 At Oursky we’re all about helping brands and entrepreneurs make their ideas happen. Get in touch if you’re looking for a partner to help build your next digital product.