90th percentile delivery time per city for peak-hour orders in SQL
Company: DoorDash
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Compute the 90th percentile delivery time in minutes (`delivery_time - order_time`) per city for orders placed in peak hours: [11:00, 14:00) and [17:00, 21:00). Only orders with a delivery record count. Return `city_name` and `p90_delivery_minutes` (rounded to 2 decimals), ordered by `city_name` ascending.
### Input Table(s)
`cities`
| Column | Type | Meaning |
|---|---|---|
| city_id | INT | City identifier. Primary key. |
| city_name | VARCHAR(100) | City name. NOT NULL and unique. |
`orders`
| Column | Type | Meaning |
|---|---|---|
| order_id | INT | Order identifier. Primary key. |
| city_id | INT | City the order was placed in. Foreign key to `cities.city_id`. |
| order_time | DATETIME (`TIMESTAMP` in PostgreSQL) | When the order was placed. NOT NULL. |
`deliveries`
| Column | Type | Meaning |
|---|---|---|
| delivery_id | INT | Delivery identifier. Primary key. |
| order_id | INT | Order that was delivered. Foreign key to `orders.order_id`. |
| delivery_time | DATETIME (`TIMESTAMP` in PostgreSQL) | When the order was delivered. NOT NULL. |
Keys: `cities.city_id`, `orders.order_id` and `deliveries.delivery_id` are primary keys. `orders.city_id` references `cities`, and `deliveries.order_id` references `orders`, with at most one delivery per order.
### Output Contract
| Column | Type | Meaning |
|---|---|---|
| city_name | VARCHAR | The city's name. |
| p90_delivery_minutes | NUMERIC | The 90th percentile of the city's peak-hour delivery times in minutes, rounded to 2 decimal places. |
The percentile uses the nearest-rank definition: sort the city's delivery times ascending and take the value at 1-based position `CEIL(0.9 * n)`, where `n` is the number of qualifying deliveries in that city. Return one row per city that has at least one qualifying delivery, ordered by `city_name` ascending (names are unique, so the order is unique).
### Example
`cities`
| city_id | city_name |
|---|---|
| 1 | Austin |
| 2 | Boston |
| 3 | Chicago |
`orders`
| order_id | city_id | order_time |
|---|---|---|
| 1 | 1 | 2024-03-01 11:05:00 |
| 2 | 1 | 2024-03-01 12:30:00 |
| 3 | 1 | 2024-03-01 17:10:00 |
| 4 | 1 | 2024-03-02 20:40:00 |
| 5 | 1 | 2024-03-02 15:00:00 |
| 6 | 1 | 2024-03-02 18:00:00 |
| 7 | 2 | 2024-03-01 13:20:00 |
| 8 | 2 | 2024-03-01 19:00:00 |
| 9 | 2 | 2024-03-01 14:00:00 |
| 10 | 3 | 2024-03-01 10:59:00 |
`deliveries`
| delivery_id | order_id | delivery_time |
|---|---|---|
| 101 | 1 | 2024-03-01 11:25:00 |
| 102 | 2 | 2024-03-01 13:00:00 |
| 103 | 3 | 2024-03-01 17:55:00 |
| 104 | 4 | 2024-03-02 21:05:00 |
| 105 | 5 | 2024-03-02 16:30:00 |
| 107 | 7 | 2024-03-01 13:52:30 |
| 108 | 8 | 2024-03-01 19:18:00 |
| 109 | 9 | 2024-03-01 15:00:00 |
| 110 | 10 | 2024-03-01 11:30:00 |
Expected result:
| city_name | p90_delivery_minutes |
|---|---|
| Austin | 45.00 |
| Boston | 32.50 |
Austin's qualifying deliveries take 20, 30, 45 and 25 minutes (orders 1 to 4). Order 5 was placed at 15:00, outside both windows, and order 6 has no delivery. With `n = 4`, position `CEIL(3.6) = 4` in the sorted list 20, 25, 30, 45 is 45. Boston keeps orders 7 (32.5 minutes) and 8 (18 minutes); order 9 was placed at exactly 14:00, which the [11:00, 14:00) window excludes. Position `CEIL(1.8) = 2` gives 32.50. Chicago's only order was placed at 10:59, so Chicago has no row.
### Constraints and Clarifications
- `orders` has between 1 and 10,000 rows. Each order has at most one delivery record.
- `order_time` is always before `delivery_time`, and neither is NULL.
- `city_name` values are unique.
- Peak hours are judged on the clock time of `order_time` as stored. The windows are half-open: 11:00:00 and 13:59:59 are in, 14:00:00 is out, and likewise for 17:00 to 21:00. When the delivery happens does not matter, so an order placed at 20:40 and delivered at 21:05 counts.
- Delivery minutes are the full elapsed time divided by 60 seconds, with the fractional part kept (32 minutes 30 seconds is 32.5), and are rounded only in the final output.
- Orders with no delivery record are excluded, and cities with no qualifying delivery produce no row.
- With fewer than 10 qualifying deliveries the nearest-rank 90th percentile is the city's maximum. With 20 it is the 18th smallest value.
```hint Find the right position
Work out which position in a city's sorted list of delivery times the contract asks for, then look for an aggregate or window function that returns the value at that position without interpolating.
```
Overview: Write a SQL query that finds the 90th percentile delivery time in minutes for each city, counting only delivered orders placed during the lunch and dinner peak windows. It tests half-open time-of-day filters, interval arithmetic and choosing a percentile function that returns a nearest-rank value.
Compute the 90th percentile delivery time in minutes (`delivery_time - order_time`) per city for orders placed in peak hours: [11:00, 14:00) and [17:00, 21:00). Only orders with a delivery record count. Return `city_name` and `p90_delivery_minutes` (rounded to 2 decimals), ordered by `city_name` ascending.
Tables:
- `cities(city_id INTEGER PRIMARY KEY, city_name VARCHAR(100) NOT NULL UNIQUE)`: one row per city.
- `orders(order_id INTEGER PRIMARY KEY, city_id INTEGER, order_time TIMESTAMP NOT NULL)`: `city_id` is the city the order was placed in and references `cities.city_id`; `order_time` is when the order was placed.
- `deliveries(delivery_id INTEGER PRIMARY KEY, order_id INTEGER UNIQUE, delivery_time TIMESTAMP NOT NULL)`: `order_id` is the order that was delivered and references `orders.order_id`, with at most one delivery per order; `delivery_time` is when the order was delivered.
Rules:
- `orders` has between 1 and 10,000 rows, and each order has at most one delivery record.
- `order_time` is always before `delivery_time`, and neither is NULL. `city_name` values are unique.
- Peak hours are judged on the clock time of `order_time` as stored. The windows are half-open: 11:00:00 and 13:59:59 are in, 14:00:00 is out, and likewise for 17:00 to 21:00. When the delivery happens does not matter, so an order placed at 20:40 and delivered at 21:05 counts.
- Delivery minutes are the full elapsed time divided by 60 seconds, with the fractional part kept (32 minutes 30 seconds is 32.5), and are rounded only in the final output.
- Orders with no delivery record are excluded, and cities with no qualifying delivery produce no row.
- The percentile uses the nearest-rank definition: sort the city's delivery times ascending and take the value at 1-based position `CEIL(0.9 * n)`, where `n` is the number of qualifying deliveries in that city. With fewer than 10 qualifying deliveries the nearest-rank 90th percentile is the city's maximum. With 20 it is the 18th smallest value.
Output columns, in this order: `city_name` (VARCHAR, the city's name) and `p90_delivery_minutes` (NUMERIC, the 90th percentile of the city's peak-hour delivery times in minutes, rounded to 2 decimal places). Return one row per city that has at least one qualifying delivery, ordered by `city_name` ascending (names are unique, so the order is unique).
Tables
cities(city_id INTEGER, city_name VARCHAR(100))
orders(order_id INTEGER, city_id INTEGER, order_time TIMESTAMP)
deliveries(delivery_id INTEGER, order_id INTEGER, delivery_time TIMESTAMP)
Hints
- Decide eligibility from the clock time of `order_time` alone: each peak window includes its start time and excludes its end time, and the delivery time plays no part in it.
- Convert the full elapsed time between the two timestamps to minutes with the fraction kept, and round only the final percentile value.