Compute Courier Delivery Pay
DoorDashSoftware Engineer · Technical ScreenLast updated
This question tests the ability to handle time intervals and calculate cumulative earnings based on overlapping delivery pay rates.
Compute Courier Delivery Pay
DoorDash couriers earn different base pay rates depending on the time of day (e.g., peak hours). You are given a list of rates, where each rate is defined as (start_time, end_time, amount_per_hour), and a list of deliveries, where each delivery is (start_time, end_time).
Calculate the total earnings for a courier. A delivery earns the rate active during each portion of its duration. Assume time is represented as integers (e.g., hours 0 through 24).
Function Signature
def calculate_earnings(rates, deliveries):
# rates: List of tuples (start, end, rate)
# deliveries: List of tuples (start, end)
# Returns: Total earnings as a float
Constraints
0 <= start_time < end_time <= 24- Rates are guaranteed to be non-overlapping.
- A delivery may span multiple rate periods or fall within a single rate period.
Examples
Example 1:
rates:[(0, 10, 10), (10, 20, 20)]deliveries:[(5, 15)]- Result:
150.0(5 hours at $10/hr = $50, plus 5 hours at $20/hr = $100)
Example 2:
rates:[(0, 5, 10), (5, 10, 15)]deliveries:[(0, 2), (8, 9)]- Result:
35.0(2 hours at $10 = $20, plus 1 hour at $15 = $15)
Edge Cases
- A delivery entirely outside the defined rates (should be treated as $0).
- A delivery that spans across multiple rate changes.
- Empty lists of rates or deliveries.
Hints
Hint 1
Consider iterating through each delivery individually and then checking which parts of that delivery overlap with the provided rate intervals.
Hint 2
For each delivery interval [d_start, d_end], iterate through all rate intervals [r_start, r_end, rate] to find the intersection [max(d_start, r_start), min(d_end, r_end)].
Hint 3
Only add to the total if the calculated intersection is valid (i.e., the start of the intersection is less than the end).
Follow-up questions
- How would your approach change if the rates overlapped, and you had to take the maximum applicable rate for any given time?
- What is the time complexity of your solution, and can you optimize it if there are thousands of deliveries?
- How would you handle cases where rate intervals are not sorted by time?
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.