cswork / libraryOpen practice workspace

Coding questions · DoorDash

Compute dasher payout from API data

This question evaluates a candidate's proficiency in data manipulation and transaction aggregation using SQL or Python, encompassing time-zone-aware timestamp handling, pagination, late or missing events, cancellations and negative adjustments, rounding rules, and itemized breakdowns.

Given a REST endpoint GET /payout that returns each delivery’s components (base pay, distance/time bonuses, promotions, tips, fees, adjustments, taxes, refunds) with timestamps, compute each Dasher’s total pay for a pay period. Handle pagination, missing/late events, time zones, partial-day boundaries, cancellations/chargebacks, negative adjustments, and rounding rules. Provide a Python or SQL implementation that aggregates per Dasher and outputs totals and itemized breakdowns; include test cases and discuss complexity.

Compute Dasher Payout for a Pay Period with Component Breakdown

You are given payout events for food-delivery couriers (Dashers). Each payout event represents a single monetary component associated with a delivery or adjustment: base pay, bonuses, promotions, tips, fees, taxes, refunds, or chargebacks.

All payout events are stored in UTC. A bi-weekly pay period corresponding to 2025-05-20 to 2025-05-31 in America/Los_Angeles (UTC-7) is represented in UTC as:

  • start of pay period (inclusive): 2025-05-20 07:00:00
  • end of pay period (inclusive): 2025-06-01 06:59:59

Your task: using the tables below, write a SQL query that computes each Dasher’s payout for this pay period with an itemized breakdown by component type.

Requirements:

  1. Include only payout_events whose event_timestamp is between '2025-05-20 07:00:00' and '2025-06-01 06:59:59' (inclusive). This handles the time-zone and partial-day boundary logic.
  2. Events belong to the pay period when their event_timestamp falls in this window, even if the underlying delivery happened earlier or later (this models late tips or adjustments).
  3. Handle cancellations, chargebacks, and negative adjustments correctly by treating the amount as-is (it can be negative and should reduce pay).
  4. For each Dasher and component_type, compute the subtotal of amount and round it to 2 decimal places using standard rounding (ROUND(SUM(amount), 2)).
  5. For each Dasher, compute net_total as the sum of the rounded component subtotals (so the net_total matches the sum of the displayed breakdown numbers, even if this differs slightly from rounding the grand sum directly).
  6. Output one row per Dasher and component_type that occurs in the data, with columns:
    • dasher_id
    • dasher_name
    • component_type
    • component_total (rounded to 2 decimals)
    • net_total (sum of that Dasher’s rounded component_totals)

Use only the tables and sample data described below. Do not assume any extra tables or columns.

Table dashers

columntypeconstraints
dasher_idINTPRIMARY KEY
dasher_nameVARCHAR(50)NOT NULL

Sample data:

dasher_iddasher_name
1Alice Perez
2Bob Smith

Table payout_events

columntypeconstraints
payout_event_idINTPRIMARY KEY
dasher_idINTNOT NULL
delivery_idINT
component_typeVARCHAR(20)NOT NULL
amountDECIMAL(10,3)NOT NULL
event_timestampTIMESTAMPNOT NULL

Sample data:

amountcomponent_typedasher_iddelivery_idevent_timestamppayout_event_id
5BASE_PAY11012025-05-20 08:00:001
1.5DISTANCE_BONUS11012025-05-20 08:00:002
3TIP11012025-05-20 08:05:003
4.25BASE_PAY11022025-05-31 06:00:004
2.5TIP11022025-06-01 07:10:005
2PROMOTION11032025-05-19 23:00:006
5BASE_PAY11042025-05-30 09:00:007
-5REFUND11042025-05-30 10:00:008
-1.25ADJUSTMENT1NULL2025-05-29 12:00:009
0.75TAX1NULL2025-05-21 12:00:0010
6BASE_PAY22012025-05-20 06:30:0011
6BASE_PAY22022025-05-20 07:30:0012
4TIP22022025-05-22 09:00:0013
5.5BASE_PAY22032025-05-31 23:30:0014
-4CHARGEBACK22032025-06-01 05:00:0015
1.005TIP22042025-05-25 10:00:0016
0.335ADJUSTMENT2NULL2025-05-26 14:00:0017
0.99FEE22052025-05-28 11:00:0018
0.5TAX2NULL2025-05-29 16:00:0019

Expected output

For the pay period from '2025-05-20 07:00:00' to '2025-06-01 06:59:59' UTC, return one row per Dasher and component_type present in that window, with the rounded subtotal per component and the Dasher's net_total (sum of rounded component subtotals). The sample output below is one valid ordering.

component_totalcomponent_typedasher_iddasher_namenet_total
-1.25ADJUSTMENT1Alice Perez13.25
14.25BASE_PAY1Alice Perez13.25
1.5DISTANCE_BONUS1Alice Perez13.25
-5REFUND1Alice Perez13.25
0.75TAX1Alice Perez13.25
3TIP1Alice Perez13.25
0.34ADJUSTMENT2Bob Smith14.34
11.5BASE_PAY2Bob Smith14.34
-4CHARGEBACK2Bob Smith14.34
0.99FEE2Bob Smith14.34
0.5TAX2Bob Smith14.34
5.01TIP2Bob Smith14.34

Hints

  1. Filter payout_events by the explicit UTC timestamp window for the pay period.
  2. First aggregate and ROUND per dasher and component_type, then sum these rounded subtotals per dasher to get net_total.