Minimize batches and allocate riders by time
DoorDashMachine Learning Engineer · Technical ScreenLast updated
This problem tests your ability to optimize resource scheduling by grouping tasks with overlapping time windows into the minimum number of batches, subject to capacity constraints.
Problem Statement
You are managing a delivery platform and have a list of delivery requests. Each request has a delivery time window [start_time, end_time] and a required capacity c. You have a fleet of delivery batches, where each batch has a maximum capacity K.
A batch can handle multiple deliveries if their time windows are compatible (they can be performed concurrently) and the sum of their required capacities does not exceed K. Two delivery requests are compatible if their time intervals [s1, e1] and [s2, e2] allow them to be processed together. For this problem, assume two requests are compatible if their time windows overlap. However, to keep it simple, we define a batch as a set of requests that can all be performed within a single common time period, provided the total capacity constraint K is met.
Given a list of requests requests = [[start, end, capacity], ...] and a batch capacity K, return the minimum number of batches required to fulfill all deliveries.
Function Signature
def min_batches(requests: List[List[int]], K: int) -> int
Constraints
1 <= len(requests) <= 10^40 <= start < end <= 10^61 <= capacity <= K1 <= K <= 100
Example 1
Input: requests = [[1, 5, 2], [2, 6, 2], [7, 8, 1]], K = 3
Output: 2
Explanation: The first two requests overlap and have a total capacity of 4, which exceeds K=3, so they must be in different batches. The third request can share a batch with either.
Edge Cases
- Single request.
- All requests have the same time window.
- Total capacity of overlapping requests exactly equals
K.
Hints
Hint 1
Consider this as an interval coloring problem combined with a bin-packing constraint.
Hint 2
First, identify which tasks conflict by creating a timeline or sorting the intervals to find overlaps.
Hint 3
Use a greedy strategy to assign tasks to existing batches, checking both time compatibility and the capacity limit `K` for each batch.
Follow-up questions
- How would your approach change if each delivery also had a specific pickup location and the batches had a distance constraint?
- If you have a massive number of requests, how would you optimize the search for an available batch?
- What if the capacity `K` is not uniform across all batches?
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.