Java Challenge: Dropping Balloons in Java
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.
- Login to post comments
Comments
coworker (not verified) - Tue, 11/17/2009 - 03:56
coworker (not verified) - Wed, 11/18/2009 - 13:16
This is a more elegant version... Next challenge: How has the code to be altered to support more than 2 ballons?
dag
davidecr - David Castañeda (not verified) - Tue, 11/17/2009 - 04:23
The algorithm move incrementing the floor to test using the max number of tries available, until the first ball braks, after that go backwards (substracting the last amount added to the floors in the last operation and start adding until it finds the max floor where water ball won´t brake.
I leave the max amount of tries available inside the class that contains the algorithm after realizing that the max amount of tries depends on the number of floors available (actually for this excersise the max amount of tries available should be 14, I didn't implement how to find that number, because it's late & I'm lazy &I think somebody will tell me how to do it easyly (the first number given (1+2+3+4+n > numberOfFloors) & because I want to check my Nokia N900 order status:) )
Anonymous (not verified) - Tue, 11/17/2009 - 09:42
Trying to recall my physics, I would like to ask the following question. Could we assume that if a waterballon would break at floor n, then it would break if we dropped it from higher floors? If this is the case, then we need log2(100) attempts.
Nico Heid - Tue, 11/17/2009 - 10:03
yes, we can assume this. so 2 more spoilers for you:
as we can see, linear won't work, too many attempts.
logarithmic (binary search) won't work because you'd need more than 2 balloons. and creating more balloons will count as cheating.
so think of something else. it's a bit tricky, but that's why it's called a challenge.
antorun (not verified) - Tue, 11/17/2009 - 11:46
Dynamic programming and some precalculation.
Key function f(floor,egg) = minimal number of try need to find correct floor number. Consider 2 cases and construct idea of calculation based on floor count and egg (waterbaloon) count.
Matthias Reuter - Tue, 11/17/2009 - 12:12
I'm not much of a Java coder, so here's my Javascript solution. The algorithm basically consists of two functions, "jump", which is called until the first balloon pops, and "walk" which is called afterwards until the second balloon pops or there are no more tries left:
var numberOfTrialsLeft = 15;
// What a coincidence it is to call floor when randomly generating a floor number :-)
var highestFloor = Math.floor(Math.random() * numberOfFloors);
var balloonBreaksOnFloor = function (floorNumber) {
if (numberOfTrialsLeft === 0) {
throw "Sorry, no more trials left";
}
numberOfTrialsLeft--;
return floorNumber > highestFloor;
};
var walk = function (lastFloorThatWorked) {
if (numberOfTrialsLeft === 0) {
return lastFloorThatWorked;
}
var nextFloorToCheck = lastFloorThatWorked + 1;
if (balloonBreaksOnFloor(nextFloorToCheck)) {
return lastFloorThatWorked;
}
return walk(nextFloorToCheck);
};
var jump = function (lastFloorThatWorked) {
var nextFloorToCheck = lastFloorThatWorked + numberOfTrialsLeft;
if (balloonBreaksOnFloor(nextFloorToCheck)) {
return walk(lastFloorThatWorked);
}
return jump(nextFloorToCheck);
};
var getHighestFloorNumber = function () {
return jump(-1);
};
Matthias Reuter - Tue, 11/17/2009 - 12:18
Actually, more challenging is the following task:
Given a number of balloons and a number of trials, what is the maximum number of floors you can check?
Nico Heid - Tue, 11/17/2009 - 13:48
if you figured out the pattern for the given problem your question would lead to using on part of the puzzle solving recursively till you have only one balloon left.
so it is indeed a nice bonus for complexity. (and maybe another challenge in javascript? ) :-)
kkreja (not verified) - Tue, 11/17/2009 - 13:03
Daniel Ribeiro (not verified) - Wed, 11/18/2009 - 04:20
Whole answer, testing for all floors. Would be much cleaner in Scala/Ruby, but it is not that much code:
DannyB (not verified) - Wed, 11/18/2009 - 17:29
Classical binary search is ruled out because we can not exceed two failures but will not have reached a solution.
Having only two balloons means we can only work upward.
My first thought was to start at floor 1 and work up by adding powers of two. Therefore, we will check floors 1, 3, 7, 15, 31, 63. Continuing along this line of analysis, it eventually becomes clear that we need to be more optimal early on and add more than 2 to floor 1 to take advantage of the number of available trials remaining.
If the balloon doesn't break at floor 1, then we have used 1 trial, have 14 left and 2 balloons.
Let's add the number of remaining trials to determine the next floor to check. 14 trials left means we add 14 to 1 to check floor 15 next. If the balloon breaks at floor 15, we have used 2 trials, have 13 trials left and one balloon. We incrementally check floors 2 through 14, which uses up all 13 remaining trials. We now have our answer.
If the balloon did not break at floor 15, we have used 2 trials, have 13 trials left and 2 balloons. We add the number of trials left to 15 to get floor 28 to check next. If the balloon breaks at floor 28, we have used 3 trials, have 12 left and 1 balloon. We incrementally check floors 16 through 27 and we have our answer.
If the balloon did not break at floor 28, we have used 3 trials, have 12 left, and 2 balloons. We add the number of trials left to 28 to get floor 40 to check next. If the balloon breaks at floor 40, we have used 4 trials, have 11 left and 1 balloon. We incrementally check floors 29 throu 39 and we have our answer.
If the balloon did not break at floor 40, we have used 4 trials, have 11 left, and 2 balloons. We add the number of trials left to 40 to get floor 51 to check next. If the balloon breaks at floor 51, we have used 5 trials, have 10 left and 1 balloon. We incrementally check floors 41 through 50 and we have our answer.
If the balloon did not break at floor 51, we have used 5 trials, have 10 left, and 2 balloons. We add the number of trials left to 51 to get floor 61 to check next. If the balloon breaks at floor 61, we have used 6 trials, have 9 left and 1 balloon. We incrementally check floors 52 through 60 and we have our answer.
If the balloon did not break at floor 60, we have used 6 trials, have 9 left, and 2 balloons. We add the number of trials left to 60 to get floor 69 to check next. If the balloon breaks at floor 69, we have used 7 trials, have 8 left and 1 balloon. We incrementally check floors 61 through 68 and we have our answer.
If the balloon did not break at floor 69, we have used 7 trials, have 8 left, and 2 balloons. We add the number of trials left to 69 to get floor 77 to check next. If the balloon breaks at floor 77, we have used 8 trials, have 7 left and 1 balloon. We incrementally check floors 70 through 76 and we have our answer.
If the balloon did not break at floor 77, we have used 8 trials, have 7 left, and 2 balloons. We add the number of trials left to 77 to get floor 84 to check next. If the balloon breaks at floor 84, we have used 9 trials, have 6 left and 1 balloon. We incrementally check floors 78 through 83 and we have our answer.
If the balloon did not break at floor 84, we have used 9 trials, have 6 left, and 2 balloons. We add the number of trials left to 84 to get floor 90 to check next. If the balloon breaks at floor 90, we have used 10 trials, have 5 left and 1 balloon. We incrementally check floors 85 through 89 and we have our answer.
If the balloon did not break at floor 90, we have used 10 trials, have 5 left, and 2 balloons. We add the number of trials left to 90 to get floor 95 to check next. If the balloon breaks at floor 95, we have used 11 trials, have 4 left and 1 balloon. We incrementally check floors 91 through 94 and we have our answer.
If the balloon did not break at floor 95, we have used 11 trials, have 4 left, and 2 balloons. We add the number of trials left to 95 to get floor 99 to check next. If the balloon breaks at floor 99, we have used 12 trials, have 3 left and 1 balloon. We incrementally check floors 96 through 98 and we have our answer.
If the balloon did not break at floor 99, we have used 12 trials, have 3 left, and 2 balloons. We add the number of trials left to 99 to get floor 102 to check next. Since 102 is greater than 100, we incrementally check floors above 99 up to floor 100 and we have our answer.
It took me about an hour to work this out, without using your spoiler.
It is also clear how to generalize the algorithm.
I could now trivially write the code in Java, or a dozen other languages, but I'll leave that as an exercise.
Nico Heid - Wed, 11/18/2009 - 21:02
nice.
indeed, the coding part is not that important. it was the puzzle that also got my attention.
coyote (not verified) - Thu, 11/26/2009 - 23:00
You must have been joking writing such explanation... I mean, did you manually change the numbers in your description or did you write a program to output all those sentences. :-P
This is not an algorithm - this is almost a step by step debugging.
By the way, this was one of the questions at Google job interviews. (instead of balloons they used eggs)
http://www.businessinsider.com/answers-to-15-google-interview-questions-...
Kevin (not verified) - Fri, 11/20/2009 - 12:09
Hey, it's my answer.
http://blog.lckymn.com/2009/11/20/java-challenge-dropping-balloons-in-java/
Regards,
Kevin
Nico Heid - Sat, 11/21/2009 - 12:06
you put some effort into it. :-) very good
Re: Java Challenge: Dropping Balloons in Java &laquo; Ke (not verified) - Fri, 04/30/2010 - 20:15
[...] Java Challenge: Dropping Balloons in Java About a few days ago, I read this post, Java Challenge: Dropping Balloons in Java having an interesting [...]