Courier Daily Pay API from Overlapping Delivery Events, with Tests
Company: DoorDash
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
Build a daily pay API for delivery couriers. Given a courier ID, the API fetches that courier's delivery events for the day from an upstream service and calculates how much the courier should be paid for that day. Test coverage is part of the requirement. You write the code live while discussing it with the interviewer.
Each event has three fields used for pay: a delivery ID, the time the event happened, and a status. Events of the same delivery are matched by delivery ID. Pay for a delivery starts when the courier accepts it and ends when it is delivered or cancelled by the customer.
Pay rules:
- Overlapping deliveries are split along the timeline. When a delivery is accepted, the number of active deliveries goes up by one. The minute in which a delivery ends is still paid at the current count, and the count goes down by one after that minute.
- Both the start minute and the end minute are paid.
- Each segment of time with a constant number of active deliveries pays `minutes × active deliveries × base rate`. The day's pay is the sum over all segments.
Example: delivery A runs from minute 1 to minute 4, and delivery B from minute 3 to minute 6. Minutes 1-2 have only A active, minutes 3-4 have both active, and minutes 5-6 have only B active. The total is `(2 × 1 + 2 × 2 + 2 × 1) × base rate`, which is 8 times the base rate.
### Constraints and Clarifications
- Assume event times are whole minutes within the day, as in the example.
- The base rate is a configured amount paid per minute for each active delivery.
### Clarifying Questions
- If timestamps arrive with seconds, how are they mapped to minutes?
- What should happen when a delivery has an accept event but no delivered or cancelled event in the day's data?
- Can the upstream service return events out of order, or the same event twice?
- Can events carry statuses other than accepted, delivered and cancelled by customer, and do any of them affect pay?
- How is money represented and rounded?
### Part 1: Calculate the day's pay
Write the core function that takes a list of events and the base rate and returns the day's pay under the rules above. It must reproduce the example's total.
```hint Check boundaries by hand
Before coding, list which minutes each segment of the example covers and confirm that every minute is counted exactly once at the right count.
```
#### What This Part Should Cover
- Pairing events into one paid interval per delivery
- Inclusive start and end minutes, and correct counting where deliveries overlap
- Exact money arithmetic
### Part 2: The API around the upstream service
Expose the calculation as an API that takes a courier ID, fetches the events from the upstream service, validates them and returns the day's pay. Explain what the API does when the upstream returns no events, when the upstream request times out, and when a delivery's data is incomplete, for example a missing end event.
```hint Imagine the courier's view
For each way the upstream call or its data can go wrong, ask what the courier would be told if your API returned an amount.
```
#### Clarifying Questions for this Part
- Who calls this API, and do they need only the total or also a breakdown per delivery or segment?
#### What This Part Should Cover
- Separation between fetching, validating and calculating
- Distinct handling of empty data, timeouts and other upstream errors
- Treatment of incomplete deliveries that keeps the pay amount explainable
### Part 3: Tests
Write the tests for the calculation and the API, and explain what each one protects against.
```hint Aim at the edges
Pick cases where an off-by-one minute or a mishandled delivery would change the expected amount.
```
#### What This Part Should Cover
- Unit cases for boundaries, overlaps and both kinds of ending
- A fake upstream service covering empty, timeout and malformed responses
- At least one check that would catch an arithmetic or ordering regression
### What a Strong Answer Covers
- Clarifying boundary and missing-data cases before writing code
- A working main flow early enough to leave time to run the tests
- Readable structure with small functions and explicit types for events and intervals
- Production concerns such as logging and an audit trail for how an amount was derived
### Follow-up Questions
- If the per-minute rate depended on how many deliveries were active at once, which parts of your code would change?
- How would you compute weekly pay for every courier in a region when the upstream service is slow?
- A courier disputes their pay for a day. What would you store so you can show exactly how the amount was computed?
Overview: A code craft exercise to build a courier daily pay API: fetch a courier's delivery events from an upstream service, pair accept and end events by delivery ID, split overlapping deliveries along a minute timeline and sum minutes times active count times base rate. It tests boundary handling, upstream error handling and tests.
Read the full DoorDash Software Engineer interview experience this question came from