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-
What is it?
Having some, you want more. But when you have too much of it you feel bored. Everyone people wants it, ...Read More »Victim Murdered in Bathroom
A man is found murdered in his bathroom naked with blood scattered all around him. The bathroom was locked from ...Read More »THE GAME OF CATS AND MICE
A number of cats got together and decided to kill between them 999919 mice. Every cat killed an equal number ...Read More »Tricky addition riddle
20+20+20=60. How can you make 60 again by using the same number 3 times, but it can’t be 20?Read More »Voiceless but cries riddle
Voiceless it cries, Wingless flutters, Toothless bites, Mouth-less mutters. Who is this?Read More »Flat and Fat
I am flat when I am new. I am fat when I am in use. I release my gas when ...Read More »Make 3 squares
Make 3 squares by moving 3 clips only.Read More »1729 is a Magic Number
Why 1729 is a magic number? Answer Logically.Read More »Sister 7
There are seven sisters in a house in a village where there is no electricity or any gadget. Sister-1: Reading ...Read More »Unique Bank Puzzle
In a certain bank, a man deposits a certain amount. According to the bank policies, the amount will be doubled ...Read More »Fully grown beard in just small time
A man asked his wife if he could go to a friends party. His wife agreed, provided that he was ...Read More »Evil Magician
Once upon a time there was an evil magician. He took 3 women from their homes and turned them into ...Read More »Find The Thief
Can you find the thief by just looking at the picture below?Read More »Dress for everyone
What dress does everyone have, but no one wears?Read More »Strawberry Jam Puzzle
The Smith family is a very wealthy family that lives in a big circular home. One morning, Mr Smith woke ...Read More »Of no use
It is of no use to one, yet absolute bliss to two : The small boy gets it for nothing, ...Read More »Passing Marks
John has to secure 40% marks to pass his Engineering final exams. He got just 40 marks and failed by ...Read More »Password for Friday Puzzle
Steffi’s daughter Jazelle needs to be picked up from school every day. Steffi asks one of her colleagues to pick ...Read More »SHEIKH’S INHERITANCE PUZZLE
An Arab sheikh tells his two sons to race their camels to a distant city to see who will inherit ...Read More »Friend mysteries murder puzzle
Two friends went on for a climbing trip. But only one returned from the vacation and said that his friend ...Read More »