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-
Height and Weight
If you drop a 15 kg iron bar and a 5 kg bag of cotton from a height of 50 ...Read More »What did he put in the barrel?
A man has a barrel with filled with oil that weighs 100 pounds, and then he puts something into it. ...Read More »How many trees riddle
In the local wood there are a number of trees. On these trees there are a number of birds. The ...Read More »Which statements are true?
I am thinking of a 6-digit number. The sum of the digits is 43. And only two of the following ...Read More »Age of Jaslin ?
Jacob is 5 years old, John is also 5 and Lucika is 7. How old is Jaslin ?Read More »The Wall Street Investor
An investor trading through a discount stock broker that charges $10.00 Dollars per transaction bought 200 shares of Vola Tile ...Read More »Form the Word
It is an eleven letter word. The first, second, third and fourth letters form a banquet’s name. The fifth, sixth ...Read More »Magical circle trap puzzle
Suppose you are trapped by a witch in a magical circle of radius 100 feet. Whenever you try to move ...Read More »Count The Squares
Find the number of squares in the below-given picture.Read More »Repeat all
I say everything I hear to others. I am not an animal, nor part of the human race. I immediately, ...Read More »Guess The Number
One day, I thought of ways that can be used for creating a palindrome. So I decided that I will ...Read More »Meteor Shower
Can you protect the planets from the meteor shower? Draw nine straight lines through the box below to separate the ...Read More »He ___ up from his seat to give her a ___
What is it?Read More »Jelly Give and Take puzzle
A boy and a girl each have a bag of containing the same number of jelly beans. How many jelly ...Read More »Boolean Enlightenment
The path to enlightenment lies behind one of two doors. In front of each door stands a guard who knows ...Read More »Triangle Count Puzzle
How many triangles are there in the picture below?Read More »Can you tell who is wearing what color shirt?
At a restaurant downtown, Mr. Red, Mr. Blue, and Mr. White meet for lunch. Under their coats they are wearing ...Read More »Famous puzzle by Sam Lloyd issued in 1882. Want to try?
The Puzzle: Here is a famous prize problem that Sam Loyd issued in 1882, offering $1000 as a prize for ...Read More »Quick Mind Test
On a bright sunny day, two fathers took their son fishing in the lake. Each man and son were able ...Read More »Mysterical Question puzzle
I went out to Amsterdam this summer. I spent the entire day there asking the same question to everyone I ...Read More »