Compute Courier Delivery Pay
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to work with time-interval arithmetic, interval union/overlap reasoning, and aggregation of time-based pay, testing skills in algorithms and data handling for billing computations.
Part 1: Compute Courier Pay by Summing Each Order
Constraints
- 0 <= len(orders) <= 200000
- 0 <= startTime <= endTime <= 10^9
- 0 <= ratePerMinute <= 10^6
- The final answer fits in a 64-bit signed integer
Examples
Input: (2, [(10, 20), (20, 35)])
Expected Output: 50
Explanation: Durations are 10 and 15 minutes. Total minutes = 25, so pay = 25 * 2 = 50.
Input: (3, [(10, 20), (15, 25)])
Expected Output: 60
Explanation: Orders overlap, but in this part they are paid independently. Total minutes = 10 + 10 = 20, so pay = 20 * 3 = 60.
Input: (5, [])
Expected Output: 0
Explanation: No orders means no pay.
Input: (4, [(7, 7), (7, 10)])
Expected Output: 12
Explanation: The first order lasts 0 minutes and the second lasts 3 minutes. Total pay = 3 * 4 = 12.
Hints
- You do not need to compare one order with another in this part.
- For each order, compute its duration first, then multiply by the rate.
Part 2: Compute Courier Pay from Actual Active Working Time
Constraints
- 0 <= len(orders) <= 200000
- 0 <= startTime <= endTime <= 10^9
- 0 <= ratePerMinute <= 10^6
- The final answer fits in a 64-bit signed integer
Examples
Input: (2, [(10, 20), (15, 25)])
Expected Output: 30
Explanation: The merged active interval is (10, 25), which lasts 15 minutes. Pay = 15 * 2 = 30.
Input: (3, [(1, 5), (5, 8), (10, 12)])
Expected Output: 27
Explanation: The first two orders touch, so they form 7 active minutes from 1 to 8. The third adds 2 more minutes. Total = 9, so pay = 9 * 3 = 27.
Input: (1, [(30, 40), (10, 20), (15, 18), (20, 30)])
Expected Output: 30
Explanation: After sorting and merging, all orders combine into one active interval from 10 to 40, which is 30 minutes.
Input: (7, [])
Expected Output: 0
Explanation: No orders means no active time and no pay.
Input: (4, [(5, 5), (5, 10), (8, 8)])
Expected Output: 20
Explanation: Zero-length orders add no time. The only active span is from 5 to 10, which is 5 minutes. Pay = 5 * 4 = 20.
Hints
- Sort the intervals by start time before processing them.
- Keep track of the current merged interval; when the next order starts after it ends, add the finished interval's length to the answer.