2 Eggs and 100 floor Google Classic question
There is a building of 100 floors
-If an egg drops from the Nth floor or above it will break.
-If it’s dropped from any floor below, it will not break.
You’re given 2 eggs.
Find N
How many drops you need to make?
What strategy should you adopt to minimize the number egg drops it takes to find the solution?
We want to minimize the number of drops for the worst case, while still using an approach that works well for other scenarios. So, how can we do this? Well, we should rephrase that question and ask ourselves what is really holding us back here? The main reason why it takes such a large number of drops in the worst case with our approach above (19 drops) is because in order to test out the higher floors of the building we have to start at the lower floors of the building, and at that point we have already used a large number of drops just to get to that point.
What we should try to get with our next approach is to try to reduce the worst case scenario by trying to make all possible scenarios take the same number of drops.
What if we tried to reduce the number of drops that would be required with the linear search (with the 2nd egg) after we get to one of the higher floors? This way we counteract the fact that getting to the higher floor took so many drops, and if we use less drops for the linear search we are balancing out the worst case.
Let’s try to figure this out using some simple algebra. Suppose we drop an egg from floor x. If the egg breaks, then we would have to go through the previous x-1 floors one by one using a linear search.
But, if the egg doesn’t break, in our original algorithm we would go up x floors to find the next floor to test from. Why not just go up x-1 floors instead of x floors? This would save us 1 drop if we have to do a linear search with the 2nd egg whenever the first egg breaks – because we would be doing the linear search from floors x+1 to floor ( (x+1) + (x-1)) instead of floors x+1 to floor (x+1) + x. So, that is 1 less egg drop. This means that the next floor that should be attempted to drop from is x + (x-1) if the egg does not break from floor x. And by the same reasoning the floor after that would be x + (x-1) + (x-2) if the egg does not break on floor x + (x-1).
This would go on to form a series that looks like this:
x + (x-1) + (x-2) + (x-3) + ... + 1
The series above is what’s called a triangular series which is equal to x(x+1)/2. Because there are 100 floors in the building, we set the sum equal to 100 to solve for x:
x(x+1)/2 = 100
When the sum of the series above equals 100, we get x = 13.651, which rounds up to 14. This means that we should start from floor 14 (which is our x) and then move up x-1 (13) floors to floor 27 if the egg doesn’t break and then move up x-2 (12) floors to floor 39 and so on if the egg still does not break.
This is the number of drops required as we move up the floors in the building:
Drop | Floor |
---|---|
#1 | 14 |
#2 | 27 |
#3 | 39 |
#4 | 50 |
#5 | 60 |
#6 | 69 |
#7 | 77 |
#8 | 84 |
#9 | 90 |
#10 | 95 |
#11 | 99 |
#12 | 100 |
2 Eggs 100 Floors Worst Case Solution
The solution for the worst case in this scenario occurs when the threshold floor is floor number 14 – because we will drop the first egg on floor 14, and it will break. Then we have to test floors 1-13 with the 2nd egg to see where the egg breaks again, and the egg will not break on any of those floors. But since the egg broke on the floor 14, we can conclude that the threshold floor is floor number 14.
First thought which comes to our mind is to use binary search, we first drop Egg#1 from 50th floor, if it does not break, then try the middle of second half, if breaks then we have to try each floor in first half. But this will give worst case number of drops as 50(if it brakes on 50th floor, then we have to try from 1 to 49 floors sequentially).
Second thought is to try xth floor then 2xth floor till 100th, in this case worst case time will be (100/x)+(x-1). worst case will be when Egg #1 breaks at 100th floor then we have to try Egg #2 from (100-x)th to 99th floors. In (100/x) + (x-1) equation, with increase in x, 100/x decreases while (x-1) increases, thus we can minimize it when 100/x = (x-1), this gives x ~10, which gives worst case number as 19 drops.
But increasing the floor every time by x is not a very nice idea, as with each new increase in Egg #1 drop, we should decrease Egg #2 drops to minimize worst case number. so if we drop Egg #1 from xth floor initially, then in next turn we should try x + (x-1)th floor(to keep the worst case number same).
Thus we can say X + (x-1) + (x-2)…1 = 100
X(X+1)/2 = 100 => X=14.
So we should drop Egg #1 from 14th, then 27th, then 39th and so on.
Answer is 14:
In worst case it will take 14 egg drops to find the value of N.
This follows the below logic.
Say, the egg breaks at floor n we try to find out by going (N-1) till the first floor by doing linear search.
Say for example, I throw the egg from 10th floor, and it breaks, I wíll go to floor 1 to 9 to find out the floor..
Then I would try the same logic for every 10 floors thereby setting a worst case scenario of 19 chances.. I.e. 10,20,30,40,50,60,70,80,90,100,91,92,93,94,95,96,97,98,99
To find optimum solution, let’s try this:
If for every n, egg doesnt break, instead of going to next n, go to N-1, this would save us one drop as we are doing a linear search with second egg when egg1 breaks…
So the series would look something like this..
N + (N-1) + (N-2) + (N-3) +…+ 1
Now this is a series which is equal to N(N+1)/2
Now since it is given that the egg may or may not break from 100th floor..
We can write it as..
N(N+1)/2>=100
And n=14(approx)
So we should start from 14 then move up N-1 to 13 floor I.e. 27,39…
So the floors from where the drop needs to be done are: 14,27,39,50,60,69,77,84,90,95,99,100
So the answer is 14
In the worst case, 7 eggs are required to solve the value of N. We can do this problem by using BINARY SEARCH TREE. Initially we have to start from 50th floor. If egg broke, we have to go for 25th floor, else go for 75th floor. Now, we have to leave the egg from 25th floor. If egg broke, we have to go for 13th floor else, go for 19th floor. Similarly on 75th floor too..
If yes, go to (Lower Bound+ Middle Term)/2
else , (Middle Term + Upper Bound)/2
By within maximum of 6-7 steps, we can find the answer.
yes 50 no
yes 25 No yes 75 no
yes 13 no 38
yes 7 No 19
By this we have to draw binary search tree.
But we can break 2 eggs at maximum. So going to 25th floor after 50 is not possible.
Yes, Sarthak is right,
We have 2 eggs only and if we start from 50th floor and if egg broke,
we’ll have to start from Floor 1 to 49
That will make worst case of 51 TRIALS.
Drop one egg from the first floor. If it doesn’t break drop it from the second floor. Keep dropping until the egg breaks. The floor the egg was dropped from when it broke is N. You broke one egg and have one egg left over.
you can reduce the number of drops by starting at say 10, but you’ll have to sacrifice one egg
Drop one egg from the second floor and keep going upwards until the egg breaks.
Second floor because all eggs thrown below nth floor doesnt break.
We need minimum number of attempts
The answer is much simpler, and it is N drops. Let’s assume that the egg breaks at floor 20, then you would make a drop on floor 1, it will not break, and then again on floor 2 all the way to floor 20. On the 20th floor it will break, here we know what floor the egg break (which is also going to be the Nth drop) and we will still have one egg.
Start from the 2nd floor and then go to +2 floor every time to try the next. If first egg breaks at any level then just test the 2nd egg on the -1 floor from the current level. For example….. go in this way ….. 2nd , 4th, 6th, 8th, 10th, 12th, 14th and so on….. and lets consider the egg breaks at 14th floor then try the 2nd egg at 13th floor and if it not breaks at 13 than it is the maximum level and after that the egg will break.
its an egg guys calm down, it will even break dropping from 2 feet above the ground, as u r using oops technologies, so think accordingly.
answer is 0
Start from floor 10 and go in increments of 10 with the first egg. If it breaks then use the second egg from floor 1 to 9. The worst case scenario is 10 + 9 = 19.
We have only 2 eggs if both eggs are broke. we can’t use more eggs
this is actual algorithm to find the Nth step
- We need to start from 1st floor and throw the egg
- If egg didn’t broke move to 3rd floor (+2)
- and we can continue till 99 if egg didn’t break in 99th floor then 100th floor is Nth
- if the egg break in anywhere(ex: 57) we need to check x-1 floor too with another egg ( so 56th)
- if the egg break on 56th floor then 56 floor is N otherwise 57 is N
Your Answer
More puzzles to try-
Complete The Star
Find the missing numbers:Read More »How can this be?
A pet shop owner had a parrot with a sign on its cage that said “Parrot repeats everything it hears.” ...Read More »Ultimate Sequence Riddle
Can you find the missing letter in the sequence? S – T – I – L – ? – T ...Read More »What is the next number in the sequence?
What is the next number in the sequence? 1 11 21 1211 111221 312211Read More »Which does not fit into the pipe?
Your last good ping-pong ball fell down into a narrow metal pipe imbedded in concrete one foot deep. How can ...Read More »Fisherman riddle
A fisherman decided to become a golfer. In order to buy his membership he had to sell his boat. How ...Read More »Where is the white house?
There are three houses one is red one is blue band one is white. If the red house is to ...Read More »Party with gift
You are invited in a party with some gifts. Only certain things have to bring through. You can bring a ...Read More »Arrange Hats in Squares
You are given 16 witch hats. The hats are divided in four different colours – red, blue, green and yellow. ...Read More »Find the Odd One Out Riddle
Find the Odd One Out? 84129 32418 47632 36119 67626 72927Read More »Who am I?
If you say my name, I’ll no longer exist. What am I?Read More »Who am I?
I am a 8 letter word Many wasted time in hunting me, rather I’m in the form of hardwork. 5,6,7,8 ...Read More »Pencil writing riddle
A boy presses a side of a blue pencil to a side of a yellow pencil, holding both pencils vertically. ...Read More »White Buffalo
Holding two long poles, five fellows Chase a herd of white buffalo into a cave.Read More »What was the plan ?
Two prisoners ‘Lincoln Burrows’ and ‘Michael Scofield’ are locked in a cell. There is open window approx 30 feet above ...Read More »Word can be read the same upside down
What five letter word typed in all capital letters can be read the same upside down ?Read More »How did she die?
The body of a woman is found in bed. There are no signs of violence and no marks of any ...Read More »Hard Word Plexor
Which word does the below Plexor means ?Read More »Clean bowled one day cricket match
Let’s say that a One Day International Cricket match is going on and every Batsman is getting clean bowled on ...Read More »Sciencetific trick puzzle
You are lying on a beach with a wine bottle enjoying the bright sun. After it goes empty, your friend ...Read More »