cswork / libraryOpen practice workspace

Coding questions · DoorDash

Analyze Driver Requests for Food Delivery Orders

This question evaluates data manipulation and analytical SQL skills in the Data Manipulation (SQL/Python) domain, focusing on window functions for ranking and temporal comparison, joins, and aggregation when analyzing orders and driver requests.

ORDER_TABLE

order_id | restaurant_id | created_at | total_value

1 | 101 | 2024-06-01 12:01 | 45.50

2 | 102 | 2024-06-01 12:05 | 23.00

3 | 101 | 2024-06-01 12:10 | 30.00

REQUEST_TABLE

request_id | order_id | driver_id | offer_amount | request_time

1001 | 1 | 501 | 7.50 | 2024-06-01 12:01

1002 | 2 | 502 | 6.00 | 2024-06-01 12:05

1003 | 3 | 503 | 8.00 | 2024-06-01 12:10

Scenario

You have two tables—orders and driver requests—for a food-delivery marketplace.

Question

Return every order that has at least one driver request.

For each order, rank driver offers by amount and flag the highest.

Count orders where the latest offer is higher than the previous one and compute the average improvement.

Explain bounded vs. unbounded window frames, and show an example query using each.

Hints

JOIN the tables, use ROW_NUMBER() or RANK() OVER (PARTITION BY order_id ORDER BY offer_amount DESC), apply LAG for deltas, and illustrate ROWS BETWEEN clauses.

Rank Driver Offers per Order

Return one row per driver request for orders that have at least one request. Include order metadata, rank each request by offer amount descending within its order, and add a flag for whether the request is tied for the highest offer. Format created_at and request_time as YYYY-MM-DD HH24:MI:SS so the output is stable.

Table ORDER_TABLE

columntypeconstraints
order_idINTEGERPRIMARY KEY, NOT NULL
restaurant_idINTEGERNOT NULL
created_atTIMESTAMPNOT NULL
total_valueDECIMAL(10,2)NOT NULL

Sample data:

created_atorder_idrestaurant_idtotal_value
2024-06-01 12:01110145.5
2024-06-01 12:05210223
2024-06-01 12:10310130

Table REQUEST_TABLE

columntypeconstraints
request_idINTEGERPRIMARY KEY, NOT NULL
order_idINTEGERNOT NULL, REFERENCES ORDER_TABLE(order_id)
driver_idINTEGERNOT NULL
offer_amountDECIMAL(10,2)NOT NULL
request_timeTIMESTAMPNOT NULL

Sample data:

driver_idoffer_amountorder_idrequest_idrequest_time
5017.5110012024-06-01 12:01
5028110042024-06-01 12:03
5039110052024-06-01 12:04
5046210022024-06-01 12:05
5055.5210062024-06-01 12:06
5066.5210072024-06-01 12:08
5078310032024-06-01 12:10

Expected output

One row per driver request for orders with at least one request, including order metadata, rank of offer_amount within the order (descending), and a flag for the highest offer (rank = 1).

created_atdriver_idis_highest_offeroffer_amountoffer_rankorder_idrequest_idrequest_timerestaurant_idtotal_value
2024-06-01 12:01:0050319.001110052024-06-01 12:04:0010145.50
2024-06-01 12:01:0050208.002110042024-06-01 12:03:0010145.50
2024-06-01 12:01:0050107.503110012024-06-01 12:01:0010145.50
2024-06-01 12:05:0050616.501210072024-06-01 12:08:0010223.00
2024-06-01 12:05:0050406.002210022024-06-01 12:05:0010223.00
2024-06-01 12:05:0050505.503210062024-06-01 12:06:0010223.00
2024-06-01 12:10:0050718.001310032024-06-01 12:10:0010130.00

Hints

  1. Join orders to requests on order_id before ranking.
  2. Use a window function partitioned by order_id and ordered by offer_amount descending.
  3. Format timestamp columns explicitly when the expected result requires a fixed string format.

Latest Offer Improvement Summary

For each order, compare the latest offer_amount (by request_time) to the immediately previous offer. Return one summary row with: raising_offer_orders_count (number of orders where latest > previous) and avg_improvement (average of the positive differences, ignoring orders without a previous offer).

Table ORDER_TABLE

columntypeconstraints
order_idINTEGERPRIMARY KEY, NOT NULL
restaurant_idINTEGERNOT NULL
created_atTIMESTAMPNOT NULL
total_valueDECIMAL(10,2)NOT NULL

Sample data:

created_atorder_idrestaurant_idtotal_value
2024-06-01 12:01110145.5
2024-06-01 12:05210223
2024-06-01 12:10310130

Table REQUEST_TABLE

columntypeconstraints
request_idINTEGERPRIMARY KEY, NOT NULL
order_idINTEGERNOT NULL, REFERENCES ORDER_TABLE(order_id)
driver_idINTEGERNOT NULL
offer_amountDECIMAL(10,2)NOT NULL
request_timeTIMESTAMPNOT NULL

Sample data:

driver_idoffer_amountorder_idrequest_idrequest_time
5017.5110012024-06-01 12:01
5028110042024-06-01 12:03
5039110052024-06-01 12:04
5046210022024-06-01 12:05
5055.5210062024-06-01 12:06
5066.5210072024-06-01 12:08
5078310032024-06-01 12:10

Expected output

A single summary row. With the sample data, orders 1 and 2 have multiple offers and the latest offer is higher than the previous one for both; order 3 has only one offer and is ignored for the average. The count of raising orders is 2 and the average improvement is 1.0.

avg_improvementraising_offer_orders_count
12

Hints

  1. Use LAG(offer_amount) OVER (PARTITION BY order_id ORDER BY request_time) to get the immediately previous offer within each order.
  2. Use ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY request_time DESC) to identify the latest request per order.
  3. Aggregate only rows where improvement > 0; AVG will ignore NULL improvements automatically.

Bounded vs Unbounded Window Frames

For each driver request within an order, compute two windowed metrics over offer_amount.

Use REQUEST_TABLE. Within each order_id, order requests by request_time. Return these columns:

  • order_id
  • request_id
  • request_time: formatted as YYYY-MM-DD HH24:MI
  • offer_amount
  • moving_avg_offer_last2: average offer over the current row and one preceding row in the same order
  • running_max_offer: maximum offer from the first row in the order through the current row

Order by order_id, then request_time.

Table ORDER_TABLE

columntypeconstraints
order_idINTEGERPRIMARY KEY, NOT NULL
restaurant_idINTEGERNOT NULL
created_atTIMESTAMPNOT NULL
total_valueDECIMAL(10,2)NOT NULL

Sample data:

created_atorder_idrestaurant_idtotal_value
2024-06-01 12:01110145.5
2024-06-01 12:05210223
2024-06-01 12:10310130

Table REQUEST_TABLE

columntypeconstraints
request_idINTEGERPRIMARY KEY, NOT NULL
order_idINTEGERNOT NULL, REFERENCES ORDER_TABLE(order_id)
driver_idINTEGERNOT NULL
offer_amountDECIMAL(10,2)NOT NULL
request_timeTIMESTAMPNOT NULL

Sample data:

driver_idoffer_amountorder_idrequest_idrequest_time
5017.5110012024-06-01 12:01
5028110042024-06-01 12:03
5039110052024-06-01 12:04
5046210022024-06-01 12:05
5055.5210062024-06-01 12:06
5066.5210072024-06-01 12:08
5078310032024-06-01 12:10

Expected output

One row per request with a bounded two-point moving average and an unbounded running maximum within each order.

moving_avg_offer_last2offer_amountorder_idrequest_idrequest_timerunning_max_offer
7.57.5110012024-06-01 12:017.5
7.758110042024-06-01 12:038
8.59110052024-06-01 12:049
66210022024-06-01 12:056
5.755.5210062024-06-01 12:066
66.5210072024-06-01 12:086.5
88310032024-06-01 12:108

Hints

  1. Partition both windows by order_id.
  2. Use ROWS BETWEEN 1 PRECEDING AND CURRENT ROW for the two-point moving average.
  3. Use ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW for the running maximum.