Recently I read about a nice article in one of my monthly magazines. I won’t give you the author right now, otherwise googling for the solution would be too easy.
He stated it as a possible interview question, but I really like it for the algorithm side of it. It reminded my strongly of some of the
examples given in the book How Would You Move Mount Fuji? Which is excellent by the way.
So your task is to write a Java function which computes the following:
You’re in possesion of two water balloons. In front of you is a building which is 100 stories high. As we’re programmers there is a floor 0 (that’s ok for US I guess but you have to mention that to Europeans). The challenge: Give an algorithm that tells you the highest floor where you can drop a waterballoon without breaking it. In the progress it’s ok if both waterballons break, if after that you can name the correct floor. Because that’s still easy, eg. we just start out with one balloon at floor 0 and work our way up, here’s the real callenge: You have a maximum of 15 tries.
Append the answer in the comments. Enclose you code with the <java>your_code_here</java> tags so it gets syntax highilghting.
If nobody posts the right answer it will be published as Xmas present.
Happy coding 😉
SPOILERS on the second page.
SPOILER 1: As David Castañeda already stated, the number of tries available depends on the number of floors. So the real trick is to find a mathematic model which segments the available floors in a way, that you only need a certain amount of tries (won’t give you the formula right here). So as David, again, stated correctly, it’s actually only 14 for a 100 floor building.