A phone maker's test lab drops prototype phones from the floors of a tall building. There is some unknown critical floorT between 0 and floors: a phone dropped from floor T or below survives and can be dropped again, while a phone dropped from any floor above T breaks and is gone. T = 0 means every drop breaks the phone, and T = floors means none does.
The lab has phones identical phones and wants a strategy that always finds T, whatever it is, using as few drops as possible in the worst case. Each drop can depend on the results of the earlier ones.
Write two functions:
minDrops(phones, floors) returns the minimum number of drops that guarantees finding T.
maxFloors(phones, drops) returns the largest number of floors for which drops drops and phones phones always suffice.
Rules:
phones is a positive safe integer. floors is an integer from 0 to 1,000,000,000. drops is an integer from 0 to 50 (so the answer stays a safe integer). Otherwise throw a TypeError.
A building with 0 floors needs 0 drops.
The tests use a billion floors. The textbook table over every (phones, floors) pair, which tries every floor for the first drop, is O(phones × floors²) and will not finish.
minDrops(1, 10); // 10: with one phone you must try floors 1, 2, 3, ... in orderminDrops(2, 10); // 4minDrops(2, 100); // 14minDrops(3, 100); // 9maxFloors(2, 14); // 105: 14 drops with two phones cover a 105-floor building
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
2
code listings
6
edge cases
What's inside
The full problem, with examples
Tests that check your solution
Step-by-step
Approach
Solution
Complexity
Edge cases
Follow-up questions
Card worldwide
UPI and net banking in India
One-time payment, nothing renews
Private, and saved in this browser as you type. All your notes are on My lists.