Facebook Interview Question
279 Interview Reviews |
Back to all Facebook Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Facebook:
You have two lightbulbs and a 100-storey building. You want to find the floor at which the bulbs will break when dropped. Find the floor using the least number of drops.
| Tags: | brain teaser See more , See less 8 |
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (23)
2 of 15 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
7 of 8 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
0 of 12 people found this helpful
You drop it from floor 12 next. If it breaks, you know it breaks between floors 12 and 1, inclusive. If it doesn't, you know it breaks between floors 13 and 25, inclusive.
The main principle of binary search is that with each step you reduce your search space in half. Now your search space consists of only 12 floors.
Wow, I want to get asked such a question in an interview!
Helpful Answer?
Yes |
No
Inappropriate?
6 of 6 people found this helpful
if you broke it on 50 and 25... you are out of luck and out of bulbs...
Helpful Answer?
Yes |
No
Inappropriate?
10 of 11 people found this helpful
if it doesn't break, then try floor 31 and if it breaks, then try 17 - 30 (so 16 tries, including the try on floor 16).
And on and on (45, 58, 70, 81, 91, 100). If you reach 91, you'll have tried 7 floors so far and if it doesn't break, then there's 9 more tries to get to 100 (thus 16 in the worst case)
Helpful Answer?
Yes |
No
Inappropriate?
1 of 6 people found this helpful
when first breaks, you know X(last but one fall - success) and Y(last fall - failure).
now do a linear search starting from X(conservative but accurate second step - slow).
complexity = in between logN and N.
Helpful Answer?
Yes |
No
Inappropriate?
0 of 7 people found this helpful
The complexity of binary search is logN . So it will be log(100) < 7.
Based on my experience, I think it will be floor 1 itself .
Helpful Answer?
Yes |
No
Inappropriate?
3 of 8 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
1 of 2 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
0 of 4 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
10 of 10 people found this helpful
* Drop first bulb at 14th, 27th, 39th, 50th, 60th, 69th, 78th, 85th, 91st, 96th, and (optionally) 100th until it breaks.
* Go to the highest floor where the first bulb didn't break.
* Repeatedly go up one floor and drop the second bulb. When it breaks, you have your answer.
Why is 14 optimal? Since you are decrementing each time, you want (n) such that sum(1..n) barely reaches 100. The answer is n=14. Generally, if the number of floors is (f), the lowest number of drops is ceil((sqrt(8*f+1)-1)/2).
This is the best worst-case scenario. An interesting related question is what gives the lowest expected number of drops.
And no, I could not have gotten this in an interview.
Helpful Answer?
Yes |
No
Inappropriate?
2 of 2 people found this helpful
In practice, however, take the nature of the problem into account: Start on the first floor and move up by one floor. That's the answer I would be looking for.
Helpful Answer?
Yes |
No
Inappropriate?
0 of 2 people found this helpful
Doesnt break? then floor 4
Doesnt break? keep dropping the same bulb moving even number of floors all the way upto floor 100.
If on some even floor the bulb breaks drop the second bulb from the odd floor below the even floor, to detect if its the even or the odd floor that breaks the bulb
Best case detection: 2 tries (first bulb breaks on 2nd floor, second bulb breaks on 1st floor)
Worst case: 51 tries (the fist bulb breaks at 100 and second bulb breaks or does not break at 99th floor.. )
Helpful Answer?
Yes |
No
Inappropriate?
1 of 2 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
0 of 2 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
0 of 1 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
1 of 2 people found this helpful
They didn't prohibit the use of any tools.
Grab a scale, figure out force req'd to fracture bulb. Calculate acceleration due to gravity adjusting for air resistance/barometric pressure at location (trivial for anyone who took a 1yr physics course). Figure out how many meters up you need to be based on the req'd acceleration. Done....
Helpful Answer?
Yes |
No
Inappropriate?
mer's answer is correct: 14.
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
Suppose we drop at floor f, constituting one drop. If it breaks, we must make up to F(f-1, n-1) drops. If it doesn't break, we must make up to F(s-f, n) drops. Thus, for a given floor f, we have up to 1 + max { F(f-1, n-1), F(s-f, n) } drops. Optimizing over f:
F(s, n) = 1 + min{ max { F(f-1, n-1), F(s-f, n) } for f in 1..s}
Using the appropriate base cases, we have F(100, 2) = 14, as mer has given.
Another way to think about it is in terms of decision trees. We want to construct a binary decision tree with leaf nodes for floors 1..100, and at most one "left" turn per path from root to leaf. To minimize the height of the tree, we want to reduce the variance in the length of each root-to-leaf path. This suggest we try dropping the first bulb at floors of the form: a, a-1, a-2, .. a-b, where the sum a + (a-1) + .. + (a-b) is equal to or barely over 100, so that determining almost any floor (possibly excluding the top few) takes a drops. Using this approach, we get the sequence of drops that mer has suggested.
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
5 of 8 people found this helpful
by Interview Candidate: