Compute Courier Delivery Pay
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
A delivery platform has a service that can return all orders delivered by a courier.
You are given:
- `courierId`: the courier's identifier.
- `ratePerMinute`: the courier's pay rate.
- A service method such as `getOrders(courierId)` that returns a list of orders. Each order has:
- `startTime`: when the courier started working on that order.
- `endTime`: when the courier completed that order.
Assume times are represented as integer minutes and `endTime >= startTime`.
Implement a function to compute how much the courier should be paid.
Base requirement:
- Each order is paid independently.
- Total pay is the sum of `(endTime - startTime) * ratePerMinute` across all orders.
Follow-up:
- Couriers may handle multiple orders at the same time, so order intervals can overlap.
- Implement a second version that computes pay based on actual active working time, where overlapping intervals are counted only once.
- For example, if the courier has orders `[10, 20]` and `[15, 25]`, the active working time is `15` minutes, not `20` minutes.
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
You are given the pay rate for one courier and a list of that courier's delivered orders. Each order is represented by a pair `(startTime, endTime)` in integer minutes, with `endTime >= startTime`.
Each order is paid independently, even if some orders overlap in time. Compute the courier's total pay.
The total pay is:
`sum((endTime - startTime) * ratePerMinute for 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.