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.
Input: (2, [[0, 3], [1, 4]])
Expected Output: 20
Explanation: From 0 to 1 there is 1 active order, from 1 to 3 there are 2, and from 3 to 4 there is 1. Total = 1*1^2*2 + 2*2^2*2 + 1*1^2*2 = 20.
Input: (3, [[0, 2], [2, 5], [1, 3]])
Expected Output: 33
Explanation: Touching intervals do not overlap because intervals are half-open.
Input: (7, [[1, 1], [1, 2]])
Expected Output: 7
Explanation: The first order has zero length, so only the second order contributes for 1 minute.
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.
Input: (3, [[0, 1, 3, 5], [2, 2, 2, 4]])
Expected Output: 27
Explanation: The first order waits from 1 to 3 and should not affect the second order's overlap multiplier.
Input: (4, [[0, 1, 3, 4], [0, 1, 3, 4]])
Expected Output: 48
Explanation: From 1 to 3 both orders are waiting, but wait minutes are isolated, so that middle section contributes 2 orders * 2 minutes * 4 = 16, not an overlap square.
Input: (6, [[1, 1, 4, 4]])
Expected Output: 18
Explanation: This order is entirely store-wait time from 1 to 4, so it earns 3 * 6 = 18.
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.
Input: (2, [[0, 0, 0, 3], [1, 1, 1, 4]], [[1, 3]])
Expected Output: 36
Explanation: The overlap from 1 to 3 is in peak time, so those 2 minutes use doubled rate.
Input: (3, [[0, 1, 4, 5]], [[2, 5]])
Expected Output: 24
Explanation: Part of the store-wait interval and the post-pickup interval happen during peak time, so those minutes use rate 6 instead of 3.
Input: (1, [[0, 0, 0, 5]], [[1, 4], [2, 5]])
Expected Output: 9
Explanation: Peak intervals overlap, but the rate is still only doubled once on the union [1,5).
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).