Find the car model with the fastest average bridge crossing time on a given date
Company: Waymo
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Each row of `bridge_crossings` records one trip of a car across a bridge: the time the car entered and the time it left. `exit_time` is NULL when the crossing has no recorded exit.
Find the car model with the fastest average crossing time for the reference date 2024-07-15, considering only crossings where `exit_time` is not NULL. Return one row: `model`, `avg_crossing_seconds` (the average crossing time in seconds, rounded to 2 decimals) and `fastest_crossing_seconds` (the minimum crossing time in seconds). Ties on the average go to the alphabetically first model.
```hint Turning two timestamps into seconds
In PostgreSQL, subtracting one timestamp from another gives an interval. Check which function returns the total number of seconds in that interval and which one returns only its seconds field.
```
### Input Table(s)
`cars` has one row per car. Primary key: `car_id`.
| Column | Type | Meaning |
|---|---|---|
| car_id | INT | Car identifier (primary key). |
| model | VARCHAR(100) | Model name, NOT NULL. Several cars can share one model. |
| license_plate | VARCHAR(20) | Plate number, NOT NULL and UNIQUE. |
`bridge_crossings` has one row per crossing. Primary key: `crossing_id`. Foreign key: `car_id` references `cars.car_id`.
| Column | Type | Meaning |
|---|---|---|
| crossing_id | INT | Crossing identifier (primary key). |
| car_id | INT | The car that crossed, NOT NULL. |
| entry_time | TIMESTAMP | When the car entered the bridge, NOT NULL. |
| exit_time | TIMESTAMP | When the car left the bridge. NULL when no exit was recorded. |
The two time columns are DATETIME in MySQL and TIMESTAMP without time zone in PostgreSQL.
### Output Contract
- Columns, in this order: `model`, `avg_crossing_seconds`, `fastest_crossing_seconds`.
- `avg_crossing_seconds` is the mean crossing time in seconds over the model's qualifying crossings, rounded to 2 decimals.
- `fastest_crossing_seconds` is the shortest qualifying crossing time of that model, as a whole number of seconds.
- Return a single row: the model with the smallest `avg_crossing_seconds`. When several models share the smallest rounded average, return the one whose `model` sorts first in ascending order. Put another way, order by `avg_crossing_seconds` ascending, then `model` ascending, and keep the first row.
- Return no rows when no completed crossing has an `entry_time` on 2024-07-15.
### Example
`cars`
| car_id | model | license_plate |
|---|---|---|
| 1 | Civic | 7ABC101 |
| 2 | Civic | 7ABC102 |
| 3 | Model 3 | 8TES200 |
| 4 | Prius | 6PRI300 |
`bridge_crossings`
| crossing_id | car_id | entry_time | exit_time |
|---|---|---|---|
| 101 | 1 | 2024-07-15 08:00:00 | 2024-07-15 08:02:00 |
| 102 | 2 | 2024-07-15 09:10:00 | 2024-07-15 09:11:30 |
| 103 | 3 | 2024-07-15 10:00:00 | 2024-07-15 10:01:45 |
| 104 | 3 | 2024-07-15 11:00:00 | NULL |
| 105 | 4 | 2024-07-14 23:59:50 | 2024-07-15 00:00:10 |
| 106 | 4 | 2024-07-15 12:00:00 | 2024-07-15 12:02:30 |
| 107 | 3 | 2024-07-16 07:00:00 | 2024-07-16 07:01:00 |
Expected result:
| model | avg_crossing_seconds | fastest_crossing_seconds |
|---|---|---|
| Civic | 105.00 | 90 |
The qualifying crossings are 101 (120 seconds), 102 (90 seconds), 103 (105 seconds) and 106 (150 seconds). Crossing 104 has no exit, 105 entered on 2024-07-14 and 107 entered on 2024-07-16, so none of those three count. Civic averages (120 + 90) / 2 = 105.00 across its two cars, Model 3 averages 105.00 from its one completed crossing, and Prius averages 150.00. Civic and Model 3 tie on the average, and Civic sorts first.
### Constraints and Clarifications
- `cars` has at most 10,000 rows and `bridge_crossings` at most 100,000 rows.
- `entry_time < exit_time` whenever `exit_time` is not NULL.
- A crossing belongs to the reference date when its `entry_time` falls on 2024-07-15, from 00:00:00 up to but not including 2024-07-16 00:00:00. Its `exit_time` may fall on the next day.
- Crossing time is `exit_time` minus `entry_time`, in seconds. Timestamps are stored to the whole second, so every crossing time is a whole number of seconds.
- The average is taken over crossings. Every qualifying crossing of every car of a model counts once, so a car with three crossings that day weighs three times as much as a car with one.
- Crossings with a NULL `exit_time` are left out of both the average and the minimum. A model whose only crossings that day have no exit does not compete.
- Every `car_id` in `bridge_crossings` exists in `cars`. `license_plate` is not needed for the answer.
Overview: A SQL question that asks for the car model with the lowest average bridge crossing time on one date, along with that model's fastest single crossing. It tests joins, ignoring crossings with no exit time, converting timestamp differences to seconds, grouping by model and breaking ties deterministically.