Practice/DoorDash/Design and implement Dasher payout calculation system
CodingMust
You are building a payment calculation system for a food delivery platform. Drivers pick up and deliver orders, and their earnings depend on two factors: a base rate per completed delivery and a bonus multiplier based on how many deliveries they were juggling at the time of completion.
Your task is to implement a function that processes a stream of delivery events for a single driver and calculates their total payout for a shift.
Each event contains:
delivery_id: unique identifier for the deliverytimestamp: when the event occurred (integer, monotonically increasing)status: one of "accepted" (driver picked up the order), "delivered" (driver completed the delivery), or "cancelled" (delivery was cancelled)The payment rules are:
base_rate * bonus_multipliers[concurrent_count]Example 1:
` Input: events = [ {"delivery_id": "D1", "timestamp": 100, "status": "accepted"}, {"delivery_id": "D1", "timestamp": 200, "status": "delivered"} ] base_rate = 10.0 bonus_multipliers = {1: 1.0, 2: 1.5, 3: 2.0}
Output: 10.0
Explanation: Driver accepts D1 at t=100. At t=200 when D1 is delivered, there is 1 concurrent delivery (D1 itself), so payment is 10.0 * 1.0 = 10.0 `
Example 2:
` Input: events = [ {"delivery_id": "D1", "timestamp": 100, "status": "accepted"}, {"delivery_id": "D2", "timestamp": 150, "status": "accepted"}, {"delivery_id": "D1", "timestamp": 200, "status": "delivered"}, {"delivery_id": "D2", "timestamp": 250, "status": "delivered"} ] base_rate = 10.0 bonus_multipliers = {1: 1.0, 2: 1.5, 3: 2.0}
Output: 25.0
Explanation:
Example 3:
` Input: events = [ {"delivery_id": "D1", "timestamp": 100, "status": "accepted"}, {"delivery_id": "D2", "timestamp": 110, "status": "accepted"}, {"delivery_id": "D3", "timestamp": 120, "status": "accepted"}, {"delivery_id": "D1", "timestamp": 130, "status": "delivered"}, {"delivery_id": "D2", "timestamp": 140, "status": "delivered"}, {"delivery_id": "D3", "timestamp": 150, "status": "delivered"} ] base_rate = 10.0 bonus_multipliers = {1: 1.0, 2: 1.5, 3: 2.0}
Output: 55.0
Explanation: