Thursday, October 13, 2011

Dropping Eggs from a Building


You stand before a 100-story building with two eggs. Using only these two eggs, you must figure out the highest floor from which you can drop an egg such that the egg won't break when it hits the ground (we'll call this the "highest safe floor"). Every floor is equally likely to be the highest safe floor, including the top floor, and it's also just as likely that the egg will break from every floor. You can assume that if you drop an egg and it doesn't break, its shell is just as strong as it was before you dropped it.
If you want to minimize the expected number of drops you have to perform, what strategy should you use for picking which floors to drop the eggs from? You should write a program to solve this problem.

1 comment:

  1. Initially, the search space is form 1-100.

    1. Drop the egg from the middle of search space
    2. If it doesn't break, then
    i. reduce the search space to upper half(e.g. in 1st iteration, after dropping the egg, if it doesn't break, reduce search space to 51-100) .
    ii. if length of search space is >0, goto step 1, else the highest floor is the highest safe floor
    Else
    i. Keep on dropping the 2nd egg from lower-limit of search space till (mid - 1). Record the 1st floor from which it breaks. This minus 1 is the highest safe floor. If it does not break at all, mid is the highest safe floor

    ReplyDelete