Question presented to candidate: "What does "binary search on the answer" mean? Show me how you would use it to find the minimum eating speed in the Koko bananas problem, and code it in Python."
What a strong answer should cover:
- Searching the range of possible answers, not an array, with a yes/no
feasible(x)check. - The requirement: the check is monotonic, with a one-sentence reason (faster speed never needs more hours).
- Correct bounds (
lo = 1,hi = max pilefor Koko) and the right template: "first true" withhi = mid, or "last true" with a rounded-up midpoint. - Complexity O(n log M) for Koko, versus O(n ร M) for trying every speed.
- Python specifics: exact ceiling division with
(p + k - 1) // k, and that big integers make overflow a non-issue (so the focus is on the template). - Recognises other instances: ship within D days, split array largest sum, maximise the minimum distance, integer square root.
Clarifying questions expected:
- What are the limits on the pile sizes and the number of hours?
- Is there always a valid answer (at least one hour per pile)?
Code / implementation expected: Yes โ a Python feasible helper plus the binary search loop over the answer range.