Calculate Courier Earnings
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving with emphasis on temporal interval aggregation, handling overlapping events, and applying conditional rate multipliers under specified business rules.
Part 1: Calculate total courier pay with overlapping delivery intervals
Constraints
- 0 <= len(orders) <= 200000
- 0 <= rate <= 10^6
- 0 <= accepted_at <= delivered_at <= 10^9
- Orders use half-open intervals: [start, end)
- The final answer fits in a signed 64-bit integer
Examples
Input: (5, [])
Expected Output: 0
Explanation: No orders means no pay.
Input: (10, [[0, 3]])
Expected Output: 30
Explanation: One order is active for 3 minutes, so pay is 3 * 1^2 * 10 = 30.
Hints
- A minute-by-minute simulation is too slow when timestamps are large.
- The number of active orders changes only at start and end times, so process intervals between sorted event times.
Part 2: Support store-wait intervals that do not affect overlap multipliers
Constraints
- 0 <= len(orders) <= 200000
- 0 <= rate <= 10^6
- 0 <= accepted_at <= arrived_at <= picked_up_at <= delivered_at <= 10^9
- All intervals are half-open
- The final answer fits in a signed 64-bit integer
Examples
Input: (5, [])
Expected Output: 0
Explanation: No orders means no pay.
Input: (10, [[0, 2, 4, 6]])
Expected Output: 60
Explanation: Normal segments are [0,2) and [4,6), and wait is [2,4). All are paid at rate 10 with no overlaps, for 6 total minutes.
Hints
- Split each order into up to three pieces: pre-wait, wait, and post-wait.
- Store-wait pay is isolated, so you can count its contribution separately from the overlapping normal segments.
Part 3: Support doubled per-minute rates during configurable peak-hour intervals
Constraints
- 0 <= len(orders) <= 200000
- 0 <= len(peak_intervals) <= 200000
- 0 <= rate <= 10^6
- 0 <= accepted_at <= arrived_at <= picked_up_at <= delivered_at <= 10^9
- 0 <= peak_start <= peak_end <= 10^9
- All intervals are half-open
- If multiple peak intervals overlap, the rate is doubled once for covered minutes
- The final answer fits in a signed 64-bit integer
Examples
Input: (5, [], [[0, 10]])
Expected Output: 0
Explanation: Peak hours do not matter if there are no orders.
Input: (10, [[0, 2, 4, 6]], [])
Expected Output: 60
Explanation: With no peak intervals, this matches the previous problem.
Hints
- Sweep over all times where something changes: a normal segment starts or ends, a wait segment starts or ends, or a peak interval starts or ends.
- For each constant segment, the pay is segment_length * effective_rate * (normal_active^2 + wait_active).