2 Eggs and 100 floor Google Classic question

1,605.4K Views

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?

2 eggs 100 floor puzzle

Share
Add Comment

  • 15 Answer(s)

    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

    amol Scholar Answered on 21st August 2015.

    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.

    on 5th September 2015.

    But we can break 2 eggs at maximum. So going to 25th floor after 50 is not possible.

    on 2nd October 2015.

    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.

    on 3rd October 2015.
    Add Comment

    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.

    Anand Guru Answered on 12th July 2015.
    Add Comment

    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.

    SherlockHolmes Expert Answered on 21st August 2015.
    Add Comment

    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.

    aidenstoker Starter Answered on 3rd June 2016.
    Add Comment

    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

    1. We need to start from 1st floor and throw the egg
    2. If egg didn’t broke  move to 3rd floor (+2)
    3. and we can continue till 99 if egg didn’t break in 99th floor then 100th floor is Nth
    4. if the egg break in anywhere(ex: 57) we need to check x-1 floor too with another egg ( so 56th)
    5. if the egg break on 56th floor then 56 floor is N otherwise 57 is  N
    Sajan Curious Answered on 2nd September 2016.
    Add Comment

    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

    ashishPatel Starter Answered on 30th June 2016.
    Add Comment

    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.

    jaiganesh Ads Starter Answered on 10th July 2016.
    Add Comment

    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

    1. We need to start from 1st floor and throw the egg
    2. If egg didn’t broke  move to 3rd floor (+2)
    3. and we can continue till 99 if egg didn’t break in 99th floor then 100th floor is Nth
    4. if the egg break in anywhere(ex: 57) we need to check x-1 floor too with another egg ( so 56th)
    5. if the egg break on 56th floor then 56 floor is N otherwise 57 is  N
    Sajan Curious Answered on 2nd September 2016.
    Add Comment

    Am I missing something?   This is too easy.  You only need one egg.  Drop the egg from the floor #1.  If it doesn’t break go up one floor and try again.  Repeat.  When it breaks, that floor is N.

    JayPayleitner Starter Answered on 31st October 2016.
    Add Comment

    Since I have not seen the correct answer – so here it is. Remember you only have 2 egg, so finding the solution for the number of eggs required is incorrect.

    Start at floor 2 and drop the egg

    – if egg 1 breaks, try again at 1
    egg 2 breaks – answer is 0
    egg 2 survives – answer is 1

    – egg 1 does not break from floor 2 – go to floor 4
    – if egg 1 breaks at floor 4 – answer is 3 if not go to floor 6

    repeat until floor 96
    – if egg 1 survives 96 go to 99
    – if egg 1 survives 99 go to 100
    – if egg 1 does not survive 99 go to 98
    if egg 2 does not survive 98, answer is 97

    Note 96, 97, 98, 99, and 100 are all covered in the final strategy.

    R_Eaglin Starter Answered on 17th November 2016.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.
  • More puzzles to try-