Calculate a Dasher's Daily Payout
You've joined a delivery company's payouts team. Each day, a Dasher's deliveries arrive as raw text lines from the dispatch log: a delivery ID, the time it was accepted, and the time it was fulfilled. Turn a day of those lines into the Dasher's payout in cents.
The lines are messy. Some are malformed, some are duplicated, and some cross midnight. One bad line must not stop the whole day's payout, and one duplicated line must not pay twice.
What's in the codebase
payout.py. The pay constants and a stubbedcalculate_payout(lines). The docstring is the full spec. This is the only file you change.test_payout.py. Nineteen tests covering each pay component, the peak-window boundaries, rounding, the daily bonus threshold, malformed lines, duplicate IDs, midnight crossings, timestamp formats, and input mutation.scenarios.py. A sample day of seven lines, including one malformed line and one duplicate. PrintsPASSwhen the total is 2860 cents.README.md. Run commands and ground rules.
Out of the box, all 19 tests error because calculate_payout raises NotImplementedError.
Your task
Implement calculate_payout(lines) so it returns the day's payout as an integer number of cents. Each line has the form <delivery_id>,<accept_time>,<fulfil_time>, with ISO 8601 local times. The formula is defined for this lesson, not taken from DoorDash:
- Each valid delivery pays 200 cents plus 15 cents per active minute, where a partial minute rounds up.
- A delivery accepted from 11:00 up to 14:00, or from 17:00 up to 21:00, earns a 100-cent peak bonus. The accept time decides, not the fulfil time.
- A day with 8 or more valid deliveries earns a one-time 1000-cent bonus.
Skip malformed lines: the wrong number of fields, a blank ID, a timestamp that doesn't parse or has a numeric offset, or a fulfil time that isn't after the accept time. A trailing Z and surrounding whitespace are fine. For a repeated ID, the first valid line counts, and a malformed line doesn't reserve its ID. Keep all money in integers and don't mutate the input. The docstring in payout.py has the complete rules.
["D1,2026-11-16T08:05:00,2026-11-16T08:27:00"] -> 530 (22 min, off-peak) ["D1,2026-11-16T12:00:00,2026-11-16T12:20:00"] -> 600 (20 min, peak) ["D1,2026-11-16T08:05:00,2026-11-16T08:05:30"] -> 215 (30 s rounds up to 1 min) ["D1,2026-11-16T23:50:00,2026-11-17T00:10:00"] -> 500 (crosses midnight) eight 10-minute off-peak deliveries -> 3800 (2800 + 1000 bonus) ["garbage", "D1,bad,bad"] -> 0 [] -> 0
You're done when all 19 tests pass and python3 scenarios.py prints PASS.
The overlapping deliveries follow-up
A Dasher can accept a second delivery before fulfilling the first. If two deliveries both run from 12:00 to 12:20, your function pays 40 active minutes for 20 minutes of clock time. The interviewer asks whether that's right, and how you'd pay for busy time instead. Talk through who decides, what you'd need to compute it, and which deliveries would pay less.
Using AI on this problem
Screens like this usually allow AI for lookups, such as how datetime.fromisoformat treats a trailing Z in Python 3.11. Write the malformed-line policy and the duplicate rule yourself, because stating those rules is most of the signal. If AI drafts the payout loop, check it by hand against the 30-second delivery, the 10:50 accept with an 11:05 fulfil, and a malformed line followed by a valid one with the same ID.
Approach
- Split each line on commas, strip every field, and skip the line unless there are exactly three fields.
- Parse each timestamp with a helper that strips a trailing
Z, callsdatetime.fromisoformat, and rejects any result with a time zone. ReturningNonefor a bad timestamp keeps the skip logic in one place. - Validate the whole line before recording its ID: non-blank, not already seen, both timestamps parsed, and fulfil after accept. Only then add the ID to the seen set, so a malformed line never reserves it.
- Pay each valid delivery: base, minutes rounded up with integer arithmetic, and the peak bonus from the accept hour.
- Add the daily bonus once, after the loop.
The core of the loop, after the line splits into three fields with a non-blank, unseen ID:
Pythonaccept = _parse_time(accept_text) fulfil = _parse_time(fulfil_text) if accept is None or fulfil is None or fulfil <= accept: continue seen.add(delivery_id) seconds = int((fulfil - accept).total_seconds()) minutes = -(-seconds // 60) total += BASE_CENTS + minutes * PER_MINUTE_CENTS if 11 <= accept.hour < 14 or 17 <= accept.hour < 21: total += PEAK_BONUS_CENTS
-(-seconds // 60) is ceiling division without floats, so 30 seconds becomes 1 minute and exactly 600 seconds stays 10. Midnight needs no special case because both timestamps carry the date. _parse_time strips a trailing Z, calls datetime.fromisoformat, and returns None on a ValueError or when the result has a time zone. The seen set doubles as the count of valid deliveries, so the daily bonus is one check after the loop. The function runs in O(n) over the lines.
All 19 tests pass, and scenarios.py prints 2860 cents and PASS.
What to focus on
- Parse first, pay second. Most of the tests are about which lines count. Get the parser and its skip rules right, and the payout math is a few lines.
- State the skip policy out loud. Skipping bad lines is a product decision. Ask whether one bad line should fail the whole day, note that quarantining bad lines for review is a reasonable production answer, then implement what the spec says.
- Reserve an ID only after the line is valid. Adding the ID to the seen set before validation means a malformed first line blocks the valid retry.
- Round up, in integers. Floor division underpays by up to 59 seconds per delivery, and float dollars make exact equality on totals fragile.
- Peak comes from the accept time. The 10:50 accept that's fulfilled at 11:05 earns no bonus.
The overlapping deliveries follow-up
Whether overlapping minutes should pay twice is a pay-policy decision, so settle it with the business before changing code. If the answer is to pay busy time, keep the per-delivery base and peak bonus, but compute time pay from the union of the accept-to-fulfil intervals instead of their sum. That means collecting the intervals during parsing instead of adding minutes as you go, then sorting them by start time and merging any that overlap before summing.
Two deliveries from 12:00 to 12:20 count as 1200 seconds of busy time, not 2400. A delivery from 12:10 to 12:30 overlapping one from 12:00 to 12:20 counts as 1800 seconds. Deliveries done back to back pay the same as before, so only Dashers who stack orders earn less, which is worth naming as a product consequence. Rounding also changes: decide whether to round the merged total up or each interval.