Compute dasher pay from order events
Company: xAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: nan
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to process time-based event streams, manage overlapping active intervals, and compute aggregate payouts under concurrency constraints.
Constraints
- 0 <= len(events) <= 200000
- 0 <= time <= 10^9
- action is either "ACCEPT" or "FULFILL"
- The input is valid: every fulfilled order has a matching earlier or same-time accept when same-time actions are ordered as specified
Examples
Input: ([(405, 'B', 'FULFILL'), (375, 'A', 'ACCEPT'), (378, 'B', 'ACCEPT'), (396, 'A', 'FULFILL')],)
Expected Output: 14.4
Explanation: After sorting: 375-378 has 1 active order, 378-396 has 2, and 396-405 has 1. Total active-order minutes = 3 + 36 + 9 = 48, so pay = 48 * 0.30 = 14.4.
Input: ([],)
Expected Output: 0.0
Explanation: No events means no active orders and therefore no pay.
Input: ([(10, 'A', 'ACCEPT'), (10, 'A', 'FULFILL'), (15, 'B', 'ACCEPT'), (20, 'B', 'FULFILL')],)
Expected Output: 1.5
Explanation: Order A is accepted and fulfilled at the same timestamp, so it contributes no paid interval. Order B is active from 15 to 20 for 5 minutes, so pay = 5 * 0.30 = 1.5.
Input: ([(15, 'C', 'FULFILL'), (5, 'A', 'ACCEPT'), (12, 'A', 'FULFILL'), (7, 'B', 'ACCEPT'), (10, 'B', 'FULFILL'), (8, 'C', 'ACCEPT')],)
Expected Output: 5.1
Explanation: Sorted intervals: 5-7 has 1 active, 7-8 has 2, 8-10 has 3, 10-12 has 2, 12-15 has 1. Total active-order minutes = 2 + 2 + 6 + 4 + 3 = 17, so pay = 17 * 0.30 = 5.1.
Input: ([(1, 'A', 'ACCEPT'), (4, 'A', 'FULFILL'), (4, 'B', 'ACCEPT'), (6, 'B', 'FULFILL')],)
Expected Output: 1.5
Explanation: From 1 to 4, A is active for 3 minutes. At time 4, B is accepted and A is fulfilled, leaving 1 active order from 4 to 6 for 2 more minutes. Total active-order minutes = 5, so pay = 1.5.
Hints
- Sort the events by timestamp, and for equal timestamps make sure ACCEPT comes before FULFILL.
- Instead of adding floating-point pay each time, accumulate total active-order minutes first, then convert to dollars at the end.