There is a 100-story building and you are given two eggs.
The eggs (and the building) have an interesting property
that if you throw the egg from a floor number less than X,
it will not break. And it will always brake if the floor
number is equal or greater than X. Assuming that you can
reuse the eggs which didn't broke; you got to find X in a
minimal number of throws. Give an algorithm to find X in
minimal number of throws.
Answers were Sorted based on User's Feedback
Answer / j1g54w h4ck3r
Find a number n such that n(n+1)/2>=99. (You'll know why
later). n=14 in this case.
Throw one egg from 14th floor.
If it breaks,
start throwing the other egg starting from the 1st floor,
bottom up till it breaks. Max no of throws(worst case)=1+13=14.
else Throw the egg from (14+13)= 27th floor. If it breaks
start throwing the other egg from 15th floor bottom up. Max
no of throws=2+12=14.
Continue till you find the floor.
In the worst case, you'll have to do 14trials compared to
the rather large figures provided by other solutions.
Regards,
J1g54w H4ck3r
Is This Answer Correct ? | 71 Yes | 11 No |
Answer / chalimar
We are confused by the telling of this puzzle. Does the question really ask the minimum number of throws, or the minimum number of eggs? The second possibility is suggested by the clause, "Assuming that you can reuse the eggs which didn't [brake]." One answer here has made that presumption, starting at the first floor and going up floor by floor.
Is This Answer Correct ? | 0 Yes | 18 No |
Answer / ashutosh tiwari
Go to the middle floor, 50 in this case; drop egg, if breaks
go to middle of 1st and 50th i.e. 25th floor otherwise go to
middle of 50 and 100 i.e. 75th floor.
Keep on repeating the method to find the floor in minimum
iteration or egg breaking.
Hope this helps..!!!
Is This Answer Correct ? | 5 Yes | 25 No |
Answer / the practitioner
Just start on the first floor and throw the egg. If it
breaks then X=1
If not then go to the second floor and throw the egg. If it
breaks then X=2
If not then go to the third floor and throw the egg. If it
breaks then X=3
If not then keep doing that and the floor it breaks on is
the floor that X equals.
But really, in real life, a real egg would break from 3
feet.
Is This Answer Correct ? | 0 Yes | 31 No |
Answer / basvaraj s pinna
We should start from the second floor to identify the X
floor.
Step 1. Throw the one egg from second floor,
if it breaks in second floor then go to first floor,
through remaining one from the first floor
if it breaks on the first floor then X=1
else
X=2
If does not break in second floor, goto fourth floor and
throw the egg
if it breaks in fourth floor then go to third floor,
through the remaining egg from the third floor
if it breaks on the third floor then X=3
else
X=4
Similarly we should run floor = floor + 2, till one egg
breaks,
floor = floor + 2
if one egg breaks at floor then
Set currentfloor = floor -1 and check for remaining egg
throw from currentfloor, if egg breaks at currentfloor,
then X= currentfloor else X=floor
Else
floor = floor + 2
do until floor =100
Is This Answer Correct ? | 10 Yes | 55 No |
What is integer constants?
What are types of functions?
Explain what is the difference between declaring a variable and defining a variable?
why i join syntel?
23 Answers ABC, Syntel, TCS,
write a program to copy the string using switch case?
What are type modifiers in c?
What is your stream meaning?
write c program without semicolon
11 Answers MindTech, TCS, Wipro,
Explain how can I remove the trailing spaces from a string?
Can a void pointer point to a function?
Write a program to reverse a given number in c language?
Explain what is the difference between text files and binary files?