cswork / libraryOpen practice workspace

Coding questions · DoorDash

Compute dasher pay from deliveries

This question evaluates competency in data manipulation, schema design and validation, time-based aggregation, pay-computation logic, and handling of edge cases using SQL or Python.

Given a list of delivery events for dashers (e.g., dasherId, pickupTime, dropoffTime, distance, tip, and optional bonuses) and a set of pay rules (e.g., base pay per order, per-distance rate, per-minute rate, and additive tips/bonuses), implement a program that outputs each dasher’s total pay for a shift or day. Requirements: (1) Define and validate an input format and pay-rule schema; (2) Handle overlapping deliveries, missing fields, and rounding to cents; (3) Support grouping by day and by dasher, with output sorted by dasherId; (4) Provide tests covering at least three different pay configurations.

Compute dasher daily pay from deliveries and pay rules

You are given delivery events for food-delivery dashers and a set of pay rules. Each delivery record includes the dasher, pickup and dropoff timestamps, distance, tip, and an optional bonus. Each delivery row is associated with a pay configuration that defines: base pay per order, per-mile rate, and per-minute rate. Tips and bonuses are always additive.

Write a SQL query that outputs each dasher’s total pay per calendar day. Requirements:

  • Treat the work day as DATE(pickup_time).
  • Group the final output by day and dasher, and sort by dasher_id, then by date.
  • For per-minute pay, do not double-count time during overlapping deliveries. For each dasher and day, compute the total number of wall-clock minutes during which the dasher has at least one active delivery, then multiply that by the per-minute rate.
  • Use the pay configuration associated with each delivery to determine base, distance, and time pay. You may assume each dasher uses at most one pay_config_id per day.
  • Treat NULL in distance_miles, tip_amount, or bonus_amount as 0.
  • Round the final total pay to 2 decimal places (cents).

Your output should have one row per (dasher_id, work_date) with the dasher’s total daily pay.

Table pay_configs

columntypeconstraints
pay_config_idINTPRIMARY KEY
nameVARCHAR(50)NOT NULL
base_pay_per_orderDECIMAL(8,2)NOT NULL
per_mile_rateDECIMAL(8,2)NOT NULL
per_minute_rateDECIMAL(8,4)NOT NULL

Sample data:

base_pay_per_ordernamepay_config_idper_mile_rateper_minute_rate
3Standard110.1
2.5High mileage21.50.05
4No time pay310

Table deliveries

columntypeconstraints
delivery_idINTPRIMARY KEY
dasher_idINTNOT NULL
pay_config_idINTNOT NULL
pickup_timeTIMESTAMPNOT NULL
dropoff_timeTIMESTAMPNOT NULL
distance_milesDECIMAL(8,2)
tip_amountDECIMAL(8,2)
bonus_amountDECIMAL(8,2)

Sample data:

bonus_amountdasher_iddelivery_iddistance_milesdropoff_timepay_config_idpickup_timetip_amount
1101152025-05-31 10:30:0012025-05-31 10:00:004
0101232025-05-31 10:45:0012025-05-31 10:15:000
NULL101322025-05-31 11:10:0012025-05-31 11:00:002.5
0101442025-06-01 18:20:0022025-06-01 18:00:003
2101562025-06-01 18:40:0022025-06-01 18:10:001
110262.52025-05-31 09:20:0012025-05-31 09:00:00NULL
01027NULL2025-05-31 10:00:0012025-05-31 09:40:002
0102832025-06-01 12:15:0032025-06-01 12:00:001
0102922025-06-01 12:25:0032025-06-01 12:10:002

Expected output

Total pay per dasher per calendar work date (DATE(pickup_time)), including base pay per order, distance pay, non-overlapping per-minute pay, and tips/bonuses, rounded to 2 decimal places. Sorted by dasher_id, then work_date.

dasher_idtotal_paywork_date
101322025-05-31
101282025-06-01
10215.52025-05-31
102162025-06-01

Hints

  1. First aggregate deliveries per dasher and day, but to handle per-minute pay correctly you must merge overlapping time intervals so you only count each active minute once.
  2. Use window functions (LAG and a running SUM) to assign group ids to overlapping intervals, then aggregate by these groups to get merged start/end times before computing total active minutes.