Compute average crossing time and peak hourly entries from bridge checkpoint logs
Company: Visa
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Each row of `bridge_crossings` is one vehicle's complete crossing of a bridge: the time it passed the entry checkpoint and the time it passed the exit checkpoint. Write a query that returns a single row with two metrics: the average crossing time in seconds, and the largest number of vehicles that entered during any one calendar hour.
### Input Table(s)
Table `bridge_crossings`
| Column | Type | Meaning |
|---|---|---|
| crossing_id | INT | One id per crossing (primary key) |
| vehicle_plate | VARCHAR(20) | Plate of the vehicle, NOT NULL. The same plate can appear in many rows |
| entry_time | TIMESTAMP | When the vehicle passed the entry checkpoint, NOT NULL |
| exit_time | TIMESTAMP | When the vehicle passed the exit checkpoint, NOT NULL |
Primary key: `crossing_id`.
### Output Contract
Exactly one row with these columns, in this order:
- `avg_crossing_seconds`: the average of `exit_time - entry_time` in seconds over all rows, rounded to 2 decimal places.
- `peak_vehicles_per_hour`: the highest number of rows whose `entry_time` falls in the same calendar hour (for example `2024-01-15 10:00:00` through `2024-01-15 10:59:59`), as an integer.
The result is a single row, so no ordering is needed.
### Example
`bridge_crossings`
| crossing_id | vehicle_plate | entry_time | exit_time |
|---|---|---|---|
| 1 | ABC123 | 2024-01-15 10:05:00 | 2024-01-15 10:07:30 |
| 2 | XYZ789 | 2024-01-15 10:20:00 | 2024-01-15 10:21:40 |
| 3 | ABC123 | 2024-01-15 10:59:59 | 2024-01-15 11:02:00 |
| 4 | LMN456 | 2024-01-15 11:00:00 | 2024-01-15 11:03:20 |
| 5 | QRS222 | 2024-01-15 11:30:00 | 2024-01-15 11:31:00 |
Expected result:
| avg_crossing_seconds | peak_vehicles_per_hour |
|---|---|
| 126.20 | 3 |
The five durations are 150, 100, 121, 200 and 60 seconds, which sum to 631, and 631 / 5 = 126.20. Rows 1, 2 and 3 entered in the 10:00 hour (10:59:59 still belongs to it), and rows 4 and 5 entered in the 11:00 hour, so the peak is 3. Plate ABC123 entered twice in the 10:00 hour and counts twice.
### Constraints and Clarifications
- The table holds between 1 and 100,000 rows. If it were empty, the query should still return one row, with NULL in both columns, which is what `AVG` and `MAX` produce over no rows.
- No column contains NULL, and `exit_time >= entry_time` for every row.
- All timestamps share one time zone and carry whole seconds.
- A calendar hour includes the date: 10:00 on 2024-01-15 and 10:00 on 2024-01-16 are different hours.
- Count crossings (rows), not distinct plates. A vehicle that enters twice in the same hour counts twice.
- Hours are fixed clock buckets that start on the hour, not sliding 60-minute windows.
```hint Two different grains
The average is taken over all rows, but the peak needs a count per hour first. Compute each metric at its own grain, then put the two values side by side.
```
Overview: Write a SQL query over bridge crossing logs that returns the average crossing time in seconds and the highest number of vehicles entering in any calendar hour. It tests timestamp arithmetic, truncating times to the hour, and combining aggregates computed at two different grains in a single row.