
The Two Egg Problem - slig
http://www.datagenetics.com/blog/july22012/index.html
======
lunchbox
An issue with this puzzle is that the objective is not clearly stated upfront.
The author should make it clear from the outset that the goal is to minimize
the number of drops in the _worst_ case, rather than to minimize the _average_
case, especially since optimizing the average case is (in my experience) more
common in optimization problems. He asks in parentheses what the worst case
is, but that just sounds like a side question:

> The question is: What strategy should you adopt to minimize the number egg
> drops it takes to find the solution? (And what is the worst case for the
> number of drops it will take?)

Then in the solution he says:

> Problems where the solution lays lower down the building are taking less
> drops than when the solution lays higher up. _We need to come up with a
> strategy that makes things more uniform._

It is not explained _why_ it has to be more uniform.

A more minor issue with this puzzle is that for the sake of thoroughness, the
author should say is that the number of floors required to break the egg
follows a uniform distribution (i.e., every floor is an equally likely
candidate).

~~~
sampo
_optimizing the average case is (in my experience) more common in optimization
problems ... the author should say is that the number of floors required to
break the egg follows a uniform distribution_

Then again, optimizing the worst case performance can be done without worrying
about the underlying, and unknown, distribution of the lowest egg breaking
drop.

If we start optimizing the average performance, then we need to assume
something about the distribution (is it uniform? exponential? is there a bump
in the middle?), and different distribution will have different optimal search
strategies.

Looking at the worst case keeps the problem algorithmic, the version you
propose would be more an exercise in probability calculus.

~~~
SlowButEffectiv
That may be true. Lunchbox's point holds true regardless: the problem
statement is ambiguous, and is easy to correct so that it is not so.

~~~
Fargren
Well, the standard in computer science is to optimize algorithms and discuss
their performance for the worst case, so if we assume he's talking from a CS
perspective, I find it natural to assume he's talking about that. There are a
few exceptions, like QuickSort, but if some tells me to optimize some
algorithm, I'll assume he's asking for optimization of the worst case unless
he says otherwise or it's clear that the worse case is _much_ rarer than some
other case (I'd ask if I think that).

~~~
dwc
These considerations certainly exist in every day code as well. If you change
the daily reporting process to have a worst case run time of 25 hours then you
lose, even if the mean run time is cut in half.

------
hermannj314
Let's say an egg costs 1 egg (1E). This means that for management to give us 2
eggs, they were willing to spend 1 additional egg to let us make (in the
worst-case) 14 trips vs 100 if we only had 1 egg. So 1 egg < 86 trips in labor
cost.

But they weren't willing to give us a 3rd egg. This means that 1E is more than
5 trips. So our labor cost is less than E0.20/trip (assuming a uniform labor
cost per trip), but more than E0.012/trip (E1 / 86 trips).

My goal is to find someone willing to work for E1 for 100 trips. This would be
be between 1/20 and 86% of my salary (management's choice don't give me enough
information to know exactly how much I will be making in eggs).

So if I can find someone* willing to work for that labor cost, I can give him
the 1 egg problem, and I can give him the other egg to pay his salary, I then
I can spend my time doing something more interesting while still achieving
management's objectives.

*Assume the cost of finding an employee is zero. This is an important assumption to make in an interview.

~~~
malkia
This could've been something much mor expensive and dangerous to use - for
example atomic bomb test to check at what elevation when detonated it would
affect the ground (just making this up, btw)

------
kbgrant
I was asked this question at a Google interview in 2006. At that time I worked
it out mathematically as the author has here. Only afterwards did I realize
that it is a simple Dynamic Programming problem.

Let f(y) be the number of drops required with y floors remaining. At a drop
from floor x, either the egg breaks and you must test all floors from 1 to
x-1, or else the egg does not break and you have reduced to a smaller sub-
problem with y-x floors. So the number of drops required is 1 (for the current
drop) plus the max of either x-1 or f(y-x). We simply minimize this value over
all potential floors x from 1 to y.

Here is a solution in C++: <https://gist.github.com/3156157>

~~~
manglav
This is really helpful ,thank you for the example. Does anyone know of any
other resources for simple Dynamic Programming examples?

~~~
clarkm
I found the concise explanations here very helpful:
<http://people.csail.mit.edu/bdean/6.046/dp/>

------
dsr_
The blog author is very smart.

However, he is not very clever.

From my experience in the real world, I know that an unprotected egg will
smash when dropped from one story, guaranteed. No need to go up 14 or 27 or
even two stories.

(I also know that it is possible to devise protective enclosures to keep an
egg from breaking when dropped from 6 stories. The Society of Women Engineers
often sponsors competitions. Been there, got the T-shirt.)

~~~
misnome
Yeah. I dropped an egg from the kitchen counter and it broke. Ergo, first
floor it would break.

Except of course, the article doesn't mention what the landing surface is, or
whether it is what we think of as an egg, or some hypothetical super-egg. This
would seem to be discounted by the "There are no tricks, gotchas or other
devious ruses" clause, however. If the point of the article is to make it
purely a mathematical problem, then pretty much anything except an egg would
work, like "A strong box".

Otherwise, it's pretty much a "work out this problem but automatically
disregard anything that I don't think should be part of the solution"
interview question, that whilst I have no doubt are asked by many middle
manager interviewers, would imagine is a little below microsoft or google.

~~~
dsr_
All it would take to remove the real-world problem would be to change the
object from an egg to a ball made of a material of unknown strength and
toughness.

Poor specifications lie at the root of many a programming problem.

~~~
benihana
Yes but this isn't a programming problem. It's a math thought experiment.

The real thing I'm wondering is why are you making a big deal about this? You
obviously have the mental capacity to come up with the concept that it's a
special material, why can't you just pretend that's what it is and not make a
big pedantic joke about it? What is added to this discussion by pointing out
something we all know - that eggs break when you drop them from shoulder
height.

~~~
sopooneo
I hear you on that, and it can be annoying when people knowingly pretend not
to know what you are talking about. However, the most groundbreaking things
are often not so much the discovery of clever solutions as the recognition of
what the actual problem is. In my work creating custom workflow apps,
discovering the _question_ is by far the most difficult part.

------
tzs
The author states that _the_ solution is: 14, 27, 39, 50, 60, 69, 77, 84, 90,
95, 99, 100, which takes at most 14 drops.

In fact, that is _a_ solution, not _the_ solution, for this also works, and
takes at most 14 drops: 9, 22, 34, 45, 55, 64, 72, 79, 85, 90, 94, 97, 99,
100.

~~~
tzs
Darn...too late to edit.

Note that if the number of floors is 105 instead of 100, then you can still do
it in 14 drops, and the solution is unique: 14, 27, 39, 50, 60, 69, 77, 84,
90, 95, 99, 102, 104, 105.

The two solutions given above for 100 floors can be derived from the 105
solution. To get the solution given in the article, use the 105 floor solution
and if it calls for dropping from a floor above 100 pretend that you did that
drop and the egg broke, and don't actually count that drop in your drop count
since you didn't actually do the drop.

The get the solution I gave, take the 105 floor solution, and shift it down 4
floors, so it ends on 100 instead of 105.

Many more solutions are possible by mixing the ideas of pretending that floors
about 100 break, and by shifting all or part of the 105 solution down.

------
JeanPierre
From the text:

 _We now have all the information to compute the optimal two egg solution._

While I prefer to read about algorithms and strategies in the fashion the
author writes, I'm a bit sad that there's is no proof (or informal reasoning)
why this is the optimal solution. Does anyone have a paper or some reasoning
which will make me believe that this is the optimal solution?

~~~
tomp
When you break the first egg, the worst case is the difference between the
current floor (where the egg broke) and the previous floor (where the egg last
survived). So, the worst total number of drops is the number of drops for the
first egg (until it breaks), plus the above difference. If you try making the
maximum difference less than 14, you run into problems with the first
number...

The problems with optimizing worst case are much simpler than optimizing the
average case (you don't have to deal with probabilities, and you don't need to
know the distribution of egg hardness). So, the proof, and the complexity of
the proof, depends on what "optimality" measure you choose.

------
numlocked
I was asked this question in a google interview earlier this year. Neither I
nor the interviewer got past the "every 10 floors" approach (though I did make
it through that round of interviews!). Interesting to see there was more meat
on that bone.

~~~
tomp
I think that "every 10 floors" approach is optimal for average case.

Basically, _E(number of drops) = E(number of drops for 1st egg) + E(number of
drops for 2nd egg)_. We know that _E(number of drops for 2nd egg) = 1/2 the
difference between two drops of the 1st egg_ , and _E(number of drops of 1st
egg) = 1/2 the number of intervals_ , and _100 = the number of intervals × the
difference_. So, we're minimizing _x + y_ , knowing that _x × y = 100_. Since
the problem is symmetric, _x_ and _y_ must be equal, namely 10, so "every 10
floors" is the correct answer.

~~~
thaumasiotes
> E(# of drops) = E(# of drops for 1st) + E(# of drops for 2nd)

In the general case, I don't think this is true. I think you're relying on the
idea that all intervals are equally large. But I'm not definite here.

> E(# of drops for 2nd) = 1/2 the difference between two drops of the 1st egg

Sure, but this depends in a nasty way on your schedule for dropping the first
egg.

> E(# of drops of 1st) = 1/2 the number of intervals

This is definitely wrong. Say my schedule for dropping the first egg is [1, 2,
3, 100]. On your analysis, the expected number of drops the first egg will
take to break is 1.5. If we assume that the egg's breaking threshold is
uniformly distributed over floors 1-100, the actual expectation for drops-to-
break-the-egg is 3.94.

> 100 = the number of intervals × the difference

True for the average difference, but this is pretty explicit about assuming
that the size of an interval is constant.

~~~
lotharbot
>> _"E(# of drops) = E(# of drops for 1st) + E(# of drops for 2nd)"_

> _"In the general case, I don't think this is true. I think you're relying on
> the idea that all intervals are equally large."_

Pretty much.

More explicitly, the grandparent is relying on the assumption that E(egg2) is
independent of what happens to egg1, which is only true if the interval sizes
are the same. In the article's solution, E(egg2) is dependent upon which
interval egg2 is dropped in -- if egg1 breaks on the first drop, E(egg2)=7
(the center of the 1-13 interval) but if egg1 breaks on floor 100, E(egg2)=0
since we already know floor 99 is safe.

To calculate E(egg1 + egg2) we need to compute something resembling a weighted
average. A quick-and-dirty excel chart gives me an expectation of 9.97,
whereas intervals of size 10 give an expectation of 10.5 (first egg gets 1-10
drops for an expectation of 5.5; second gets 1-9 drops for an expectation of
5.0 as the tenth drop would be a repeat.)

------
majormajor
I was asked that question, with glass bulbs instead of eggs, at an interview
last year. I don't think I found the optimal solution of 14, IIRC I got to the
point (just talking aloud to the interviewer, no paper) of saying something
like "so if we started at, say, floor ten, we'd have nine floors below, and
something close to that sets of floors above if we needed to move up..." and
he said something like "ok, cool, that's good, let's move on."

I could've asked for paper, I suppose, but something about the word "puzzle"
makes me want to puzzle it out in my head.

So my question for any interviewer types who ask puzzle questions is: do you
prefer a candidate to go all the way to working out an optimal solution on
paper, or is it really more about "the approach"?

~~~
georgemcbay
In my experience it is more about "I heard you were supposed to ask questions
like this on tech interviews, so I'll ask it even if I myself am shaky enough
on the mathematics behind it that I simply know the One True Solution I read
online".

------
Confusion
What isn't explained, is why the solution that 'make(s) solutions to all
possible answers the same depth' is _also_ the solution that minimizes the
average number of attempts needed for many (sets of) eggs. The motivation
given is to reduce the worst case. That doesn't explain why there isn't
perhaps a solution with a worse worst case, but a lower average number of
drops.

~~~
btilly
Indeed it is not obvious. For example there are clearly cases where there are
solutions with the same worst case and different worst cases. And in the
slight variation where the cost of dropping an egg is the height at which you
drop it, the solutions that reduce worst cost and average cost tend to be
different.

------
jonsen
And the next question:

What strategy should you adopt to minimize the number of steps it takes you up
and down the stairs to find the solution?

~~~
psykotic
That version seems too simple to be interesting. Let there be n floors. In the
worst case you'll have to walk to the top floor from the ground floor in case
the answer happens to be n-1. Therefore you can't do any better than n-1 steps
in the worst case, so the obvious strategy of alternating dropping an egg and
walking up one floor until you break the egg is optimal and takes n-1 steps
and n-1 drops and only requires 1 egg.

~~~
jonsen
You have to go down and pick up the unbroken egg.

~~~
psykotic
Good point! That adds a lot of nuance. With several eggs, if you drop one egg
and it doesn't break, you have the option of postponing the pick-up until
later or leaving it on the ground altogether. Let me think about it.

~~~
psykotic
I had a quick go at it using memoized minimax search. I assumed that picking
up any dropped eggs is automatic when you're on the ground floor, but it would
also be easy to model it as an action.

<https://gist.github.com/3157191>

Let me know if this is what you had in mind.

------
kristopolous
I was asked this question once and I said "This is a common, widely discussed
interview question on the internet. Are you trying to find out how smart I am
thinking on my feet or whether I surf the web, because in practice, on the
job, I'd be doing the latter to find out the answer. If you have genuinely
unique problems here, you can do better than that."

And yeah, didn't get that one. Oh well. "Brain teasers" with thoroughly
documented, widely-read solutions leave you with a Bayesian problem: You'll
have the 0.5% geniuses that can come up with the answer in 2 minutes and 5% of
people that have read the answer but will make you believe that they are
geniuses by not disclosing they have read the solution.

In other words, you'd probably get 90% cheaters and 10% smarties.

------
columbo
Wouldn't the absolute best way (assuming you have never seen an egg before and
do not know anything about eggs) would be to try and crush one egg in a vice
grip to determine the strength of the outer structure and then infer a fuzzy-
ish height/speed limit from that?

~~~
chris_wot
"There are no tricks, gotchas or other devious ruses. Don’t rat-hole with
issues related to terminal velocity, potential energy or wind resistance. This
is a math puzzle plain and simple."

Not within the rules!

~~~
columbo
"There are no tricks, gotchas or other devious ruses. Don’t rat-hole with
issues related to terminal velocity, potential energy or wind resistance. This
is a math puzzle plain and simple."

Not out of the rules either (according to what you just posted). At some point
you need to crush an egg.

If the rules are implying that you can only do X and we are only looking for
solution Y then why are we even talking about this? We may as well have a
discussion about trains leaving from Chicago and New York and debate what city
they will intersect from on a given day.

~~~
chris_wot
"This is a math puzzle, plain and simple" is the give away.

------
Tichy
I blogged a solution in German in 2008:
[http://bjoernguenzel.de/2008/03/18/weiteres-matheratsel-
die-...](http://bjoernguenzel.de/2008/03/18/weiteres-matheratsel-die-zwei-
kristallkugeln/)

------
chrisbennet
If I was interviewing someone with this problem, I would want to hear what
questions the interviewee asked, such as: "Do you want to optimize for best
average or minimizing the worst case?"

------
shasta
He doesn't write down the math in the general case for the number of floors
F(e,d) that you can check with e eggs and d drops:

F(1,d) = d

F(e,d) = sum k>0\. F(e-1, d-k)

The sum can given a closed form solution for e > 2, just like for e=1 and e=2.

Another thing is that he directly solves the quadratic, but in fact n^2/2 <
n(n+1)/2 < (n+1)^2/2. So taking the square root of twice the number of floors
will get you either give you the number of drops you need with 2 eggs, or will
under count by 1. It's easy to check which that is.

------
tomerv
A more generalized version of the problem was posted as a practice question in
Google's codejam contest:

<http://code.google.com/codejam/contest/32003/dashboard#s=p2>

I suggest solving this version of the problem, as it forces you to look at the
problem from various perspectives. If you're stuck you can download solutions
from the dashboard (link in top right corner).

------
eranki
Asymptotically, the question with more coconuts is more interesting. I haven't
verified this for sure (there are some assumptions), but:

With the "naive" asymptotically correct solution (go up a constant number k of
floors, then linearly search the remainder), two coconuts gives us a worst
case cost of:

n/k + k

minimize for k, we get a cost of

2 _n^(1/2)

With 3 coconuts, we have

n/k + 2_k^(1/2)

minimize for k, we get

3 _n^(1/3)

What happens when we get log(n) coconuts?

log(n)_n^(1/log(n))

= O(log(n))

Sweet! So at least we can see that it converges to binary search as we add
coconuts.

------
pippy
This problem has multiple solutions, as demonstrated by the HN comments. It's
not asked to find a solution, instead to figure out your thought processes.
You could come up with a comp sci solution (analogous to a data structure or
algorithm), or a mathematical solution, or a physics solution or something
different.

You'd have to shape your answer to the position you're applying for. The
article would be perfect for a programming position, above and beyond what I
could ever hope to achieve.

------
soup10
This isn't a critical thinking problem, it's a math problem. As a critical
thinker, I would estimate what floor I think the egg would break at, then
estimate a safe floor I feel the egg won't break at. The more eggs I have, the
less safety margin needed. After finding the first "safe" floor I would go up
floor by floor (or more than one floor at a time if I had spare eggs).

~~~
joemoon
> then estimate a safe floor I feel the egg won't break at

This isn't a solution to the problem. Your approach carries the risk that you
never find the answer (given the preconditions). I don't think this qualifies
as "good" critical thinking, since you don't actually have a solution.

------
ww520
I would hate it to do it in an interview setting since the question didn't
state the goal of finding the worst case scenario. It's like mind reading. I
did solve it once during lunch with colleagues when we asked each other
puzzles, which was fun.

Is people ok with posting the solution here? Or want to try out some more?

Hint: square root.

~~~
vladd
Part of an interview should test communication skills as well.

In the real job's performance you'll get ill-defined specs; it's sometimes
more important to know to ask the right questions rather than solve perfectly
the wrong problems.

Interviewers are humans, sometimes tired, and an ill-defined question is
likely to appear sooner or later. Asking the interviewer if he meant worst-
case has the potential to bring lots of bonus points for you; at least it
would in my book.

~~~
ww520
Again you expect the interviewee to read your mind that he should ask a
particular question. The puzzle is set up with certain conditions and boundary
to see how the interviewee performs with those constraints. If one of the
terms of the puzzle can be changed, then other terms can be changed, too. I
would just replace the egg with a steel ball and be done with it, like just in
real life you can just push the boundary and replace the meaning of the
question completely.

------
tocomment
Is binary search until the first egg breaks and then increment by 1 for the
second egg the best approach?

~~~
Someone
No. If you first test is at floor N, the two possible consequences are:

\- egg breaks: you know you the answer is in [1,N], and will have to start
stepping by one floor until the second egg breaks

\- egg does not break: you know the answer is in [N+1, max], and you still
have an extra egg to play with.

There is an asymmetry here. Because of it, assuming that you can improve upon
binary search, N should be less than at half the maximum height. That gives
you less information, but decreases the chance that you get into the 'only one
egg in hand' endgame.

~~~
tocomment
Hmm I'm not following. What do you do with the first egg if it doesn't break?

~~~
lmm
You keep using it. Binary search is the right way of thinking about it, but in
some sense you haven't really created an even split by dropping the egg from
floor 50 - because you know that you can figure out the correct floor faster
using two eggs than one, you could check floors 51-100 much faster than you
could check floors 1-50 (Even if you just did the naive binary search approach
you first applied - i.e. if the first egg didn't break on floor 50 then drop
it from floor 75, and then after that use the second egg to check each floor,
it would only take you <=25 goes to find the right floor in 51-100, wheras it
would take you up to 50 goes to find the right floor in 1-50).

So the correct floor at which to make the first drop is clearly not floor 50.
If you dropped the first egg from floor 40 you would do better - it'd take you
up to 40 goes to find the right floor if it's below floor 40, and up to 31 or
so goes to find the right floor if it's above (because you can reuse the first
egg to check whether it's in 40-70 or 70-100). Floor 40 is in some sense a
"more binary" place to drop the first egg from than floor 50.

If you keep following this line of thought, and remember that whenever you
make your approach more efficient, you can recursively apply that more
efficient approach to "what do I do if the first egg doesn't break", you'll
arrive at the solution given in the article.

------
slig
Lots of interesting stuff in that blog, be sure to check it.

------
dahbearz
How about this solution: It's an egg. It will break from the hight of even 5
feet. The answer is the first floor.

------
thlt
As solved mathematically here <http://www.trungh.com/2010/12/light-bulbs/>

------
criveros
Start dropping the first egg at floor 1, if it doesn't break, then drop it
from floor 10, then 20, then 30, then 40, etc until it breaks. Say the egg
breaks at floor 30, start dropping the other egg from floor 21, then 22, then
23, until it breaks. Very easy problem.

