Find the Minimum Delivery Processing Speed
DoorDashSoftware Engineer · OnsiteLast updated
This problem tests binary search on the answer space to find the minimum throughput capacity required to process a set of delivery orders within a fixed deadline.
Problem Description
DoorDash manages a batch of delivery orders that must be processed before the delivery hub closes. You are given a list of integers orders, where orders[i] represents the number of items in the i-th order, and an integer hours, representing the total time available in hours.
Because orders are indivisible, if a courier processes an order at a speed of k items per hour, and an order has n items, it will take ceil(n / k) hours to complete that specific order. You must process all orders one by one, and you cannot split a single order across multiple hours in a way that exceeds the capacity k per hour. Effectively, for a chosen speed k, the time taken for any order of size n is ceil(n / k) hours.
Find the minimum positive integer speed k such that the total time taken to process all orders is less than or equal to hours. If it is impossible to complete all orders within the given hours (e.g., if the number of orders exceeds the available hours), return -1.
Function Signature
def min_delivery_speed(orders: List[int], hours: int) -> int:
Examples
Example 1:
- Input:
orders = [3, 6, 7, 11],hours = 8 - Output:
4 - Explanation: With speed 4, the times are ceil(3/4)=1, ceil(6/4)=2, ceil(7/4)=2, ceil(11/4)=3. Total hours: 1+2+2+3 = 8.
Example 2:
- Input:
orders = [30, 11, 23, 4, 20],hours = 5 - Output:
30
Constraints
1 hours, it is impossible to process them because we must complete at least one order per hour. Return -1.
Hints
Hint 1
Observe that if a speed 'k' works, any speed greater than 'k' will also satisfy the condition; this monotonicity suggests a binary search approach.
Hint 2
The lower bound for your binary search is 1, and the upper bound can be the maximum value in the 'orders' array.
Hint 3
For a fixed speed 'k', calculate the total time required by summing 'ceil(order / k)' for all orders and compare it against the given 'hours'.
Follow-up questions
- What is the time complexity of your solution, and how does it change if we use a linear search instead of binary search?
- How would the logic change if multiple couriers could work on different orders simultaneously?
- If the 'orders' list is extremely large (e.g., streaming data), how would you optimize the search for the minimum speed?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.