The Minimum Speed to Arrive on Time problem gives you a list of train ride distances and an hour limit. Each train ride except the last must take a whole number of hours (you wait for the next departure). Find the minimum integer speed such that all rides are completed within the given time. This Minimum Speed to Arrive on Time coding problem is a clean binary search on the answer application.
Apple, Amazon, and Google ask this because it is an excellent example of binary search on the answer — where you search over possible speed values and check feasibility. The array and binary search interview pattern is the key technique, and the ceiling arithmetic for wait times tests implementation precision.
Binary search on speed in the range [1, max_possible_speed]. For a given speed s, compute total time: for each ride except the last, time = ceil(dist / s). For the last ride: dist / s (exact fraction). If total time ≤ hoursBefore, speed s is sufficient — try lower. Otherwise try higher. Binary search finds the minimum feasible speed.
Distances: [1, 3, 2], hoursBefore = 6.
Distances: [1, 3, 2], hoursBefore = 2.9.
Binary search on the answer is a powerful technique for optimization problems. The key is recognizing a monotonic feasibility condition: if speed s works, all speeds > s also work. Practice identifying this monotonicity in problems about speeds, capacities, and thresholds. Equally important: implement the ceil function cleanly as (a + b - 1) / b in integer arithmetic instead of relying on floating-point library functions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Separate Squares I | Medium | Solve | |
| Adjacent Increasing Subarrays Detection II | Medium | Solve | |
| Minimum Time to Activate String | Medium | Solve | |
| Missing Element in Sorted Array | Medium | Solve | |
| Minimum Time to Complete Trips | Medium | Solve |