Calculate Daily Driver Pay
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates event-driven time-interval processing, interval-overlap arithmetic, rate computation, and robustness to malformed, duplicate, or unordered events when aggregating a driver's pay from a day's log.
Part 1: Total Driver Pay from Valid Order Intervals
Constraints
- 0 <= len(events) <= 200000
- Timestamps and base rates are integers in the range [0, 10^9]
- Events may appear in any order
- Ignore malformed orders: missing start or complete, duplicate start/complete, missing start rate, or complete timestamp <= start timestamp
Examples
Input: [{'order_id': 'A', 'event_type': 'complete', 'timestamp': 25}, {'order_id': 'B', 'event_type': 'start', 'timestamp': 0, 'base_rate_per_minute': 3}, {'order_id': 'A', 'event_type': 'start', 'timestamp': 10, 'base_rate_per_minute': 2}, {'order_id': 'B', 'event_type': 'complete', 'timestamp': 5}]
Expected Output: 45
Explanation: Order A pays (25 - 10) * 2 = 30. Order B pays (5 - 0) * 3 = 15. Total = 45.
Input: [{'order_id': 'A', 'event_type': 'start', 'timestamp': 0, 'base_rate_per_minute': 2}, {'order_id': 'A', 'event_type': 'complete', 'timestamp': 10}, {'order_id': 'B', 'event_type': 'start', 'timestamp': 5, 'base_rate_per_minute': 4}, {'order_id': 'C', 'event_type': 'start', 'timestamp': 8, 'base_rate_per_minute': 5}, {'order_id': 'C', 'event_type': 'complete', 'timestamp': 3}, {'order_id': 'D', 'event_type': 'start', 'timestamp': 1, 'base_rate_per_minute': 7}, {'order_id': 'D', 'event_type': 'complete', 'timestamp': 6}, {'order_id': 'D', 'event_type': 'complete', 'timestamp': 9}]
Expected Output: 20
Explanation: Only A is valid. B is missing a completion, C completes before it starts, and D has duplicate completion events.
Input: []
Expected Output: 0
Explanation: No events means no valid paid intervals.
Input: [{'order_id': 'X', 'event_type': 'created', 'timestamp': 0}, {'order_id': 'X', 'event_type': 'start', 'timestamp': 10, 'base_rate_per_minute': 4}, {'order_id': 'X', 'event_type': 'picked_up', 'timestamp': 12}, {'order_id': 'X', 'event_type': 'complete', 'timestamp': 15}]
Expected Output: 20
Explanation: Unrelated event types are ignored. The valid interval is [10, 15), so pay is 5 * 4 = 20.
Hints
- Group events by order_id and track how many 'start' and 'complete' events each order has.
- Only add pay for orders that end up with exactly one valid start, exactly one valid completion, and start_timestamp < complete_timestamp.
Part 2: Total Driver Pay with Peak-Period Double Rates
Constraints
- 0 <= len(events) <= 200000
- 0 <= len(peak_periods) <= 200000
- Timestamps and base rates are integers in the range [0, 10^9]
- Ignore malformed orders as defined in Part 1
- Ignore invalid peak periods where start >= end
Examples
Input: ([{'order_id': 'A', 'event_type': 'start', 'timestamp': 0, 'base_rate_per_minute': 3}, {'order_id': 'A', 'event_type': 'complete', 'timestamp': 10}], [(2, 5), (7, 9)])
Expected Output: 45
Explanation: Order A lasts 10 minutes at rate 3. Peak overlap is 3 minutes from [2,5) and 2 minutes from [7,9), total 5 peak minutes. Pay = 3 * (10 + 5) = 45.
Input: ([{'order_id': 'A', 'event_type': 'start', 'timestamp': 1, 'base_rate_per_minute': 2}, {'order_id': 'A', 'event_type': 'complete', 'timestamp': 8}, {'order_id': 'B', 'event_type': 'start', 'timestamp': 6, 'base_rate_per_minute': 1}, {'order_id': 'B', 'event_type': 'complete', 'timestamp': 10}], [(0, 3), (2, 6)])
Expected Output: 28
Explanation: Peak ranges merge to [0,6). Order A overlaps 5 peak minutes, so pay is 2 * (7 + 5) = 24. Order B is [6,10), which does not overlap [0,6) under half-open interval rules, so it pays 4. Total = 28.
Input: ([{'order_id': 'A', 'event_type': 'start', 'timestamp': 5, 'base_rate_per_minute': 2}, {'order_id': 'A', 'event_type': 'complete', 'timestamp': 10}, {'order_id': 'B', 'event_type': 'start', 'timestamp': 4, 'base_rate_per_minute': 3}, {'order_id': 'B', 'event_type': 'complete', 'timestamp': 3}, {'order_id': 'C', 'event_type': 'start', 'timestamp': 0, 'base_rate_per_minute': 1}], [(6, 8), (9, 9), (12, 11)])
Expected Output: 14
Explanation: Only A is a valid order. Only peak period (6,8) is valid. Order A has duration 5 and peak overlap 2, so pay is 2 * (5 + 2) = 14.
Input: ([], [(0, 5)])
Expected Output: 0
Explanation: No events means no pay.
Input: ([{'order_id': 'Z', 'event_type': 'start', 'timestamp': 2, 'base_rate_per_minute': 5}, {'order_id': 'Z', 'event_type': 'complete', 'timestamp': 6}], [])
Expected Output: 20
Explanation: With no peak periods, the calculation falls back to normal base pay: (6 - 2) * 5 = 20.
Hints
- First merge the peak periods so you work with disjoint sorted ranges.
- After merging, compute how many minutes of each valid order fall inside peak time, then add base pay plus one extra base-rate copy for the overlapped minutes.