cswork / libraryOpen practice workspace

Coding questions · DoorDash

Measure Customers Ordering from Bottom-Quartile Restaurants

A PostgreSQL interview problem about measuring customers who order from bottom-quartile restaurants. Candidates must define the restaurant population and quartile grain, handle ties and sparse activity, then calculate a distinct-customer share with the correct denominator.

Write one PostgreSQL SELECT statement or CTE query. Do not create, alter, or modify tables.

Schema

delivery_orders

columntype
delivery_idinteger
order_place_timetimestamp
restaurant_idinteger
customer_idinteger

order_value

columntype
delivery_idinteger
order_totalreal

Data Guarantees

  • delivery_orders.delivery_id is unique, and every 2021 delivery has exactly one matching row in order_value.
  • order_place_time, restaurant_id, customer_id, and order_total are non-NULL for those matched deliveries.
  • Every calendar month in 2021 has at least one matched delivery.

Task

Use deliveries placed on or after 2021-01-01 and before 2022-01-01. For each calendar month:

  1. Cast each matched order_total to numeric before aggregation, then sum those numeric values by restaurant to calculate each restaurant's monthly sales. Use these numeric sums when assigning sales buckets.
  2. Assign the restaurants that had at least one matched delivery that month to four sales buckets with NTILE(4), ordered by monthly sales ascending and then by restaurant_id ascending for deterministic ties.
  3. Count all distinct customers with a matched delivery that month.
  4. Count the distinct customers who placed at least one order from a restaurant assigned to bucket 1 that month.

Required Output

Return exactly these columns:

columnmeaning
order_monthThe first date of the calendar month, as a date
bottom_quartile_customer_pct100 * the number of distinct customers with at least one bucket-1 order that month, divided by all distinct customers with a matched delivery that month, rounded to two decimal places and returned as numeric(5,2)

Return exactly 12 rows, one for each month of 2021, sorted by order_month ascending.

Constraints

  • A customer who orders from several bucket-1 restaurants counts once in the numerator.
  • A customer who orders from both bucket-1 and other restaurants still counts once in the numerator and once in the denominator.
  • Use the restaurant-ID tie-break when assigning buckets; do not rank restaurants by individual order values.

Measure Customers Ordering from Bottom-Quartile Restaurants

Table delivery_orders

columntypeconstraints
delivery_idINTEGERPRIMARY KEY
order_place_timeTIMESTAMPNOT NULL
restaurant_idINTEGERNOT NULL
customer_idINTEGERNOT NULL

Sample data:

customer_iddelivery_idorder_place_timerestaurant_id
10110012021-01-01 00:00:0011
10210022021-01-10 12:00:0012
10310032021-01-20 18:30:0013
10410042021-01-31 23:59:5914
10510052021-02-15 09:00:0021
10610062021-03-15 09:00:0031
10710072021-04-15 09:00:0041
10810082021-05-15 09:00:0051
10910092021-06-15 09:00:0061
11010102021-07-15 09:00:0071
11110112021-08-15 09:00:0081
11210122021-09-15 09:00:0091
11310132021-10-15 09:00:00101
11410142021-11-15 09:00:00111
11510152021-12-31 23:59:59121

Table order_value

columntypeconstraints
delivery_idINTEGERPRIMARY KEY
order_totalREALNOT NULL

Sample data:

delivery_idorder_total
10011.25
10022.5
10033.75
10045
100515
100616
100717
100818
100919
101020
101121
101222
101323
101424
101525

Expected output

order_monthbottom_quartile_customer_pct
2021-01-0125
2021-02-01100
2021-03-01100
2021-04-01100
2021-05-01100
2021-06-01100
2021-07-01100
2021-08-01100
2021-09-01100
2021-10-01100
2021-11-01100
2021-12-01100

Hints

  1. Keep the restaurant-month sales calculation separate from customer-level counting.
  2. Apply the stated date interval exactly and preserve distinct-customer semantics in both counts.
  3. Use every specified ordering key when assigning monthly sales buckets.