Sensors on each bridge log the moment a car enters and the moment it leaves. Find the single fastest crossing across all bridges and all cars, where speed is
speed_kmh = (length_meters / travel_time_seconds) * 3.6
and travel_time_seconds is the time between entry_time and exit_time. Return the car's id, model and license plate together with the travel time and the speed.
Only crossings with a recorded exit_time and a travel time greater than 0 seconds count.
Input Table(s)
cars
| column | type | meaning |
|---|
| car_id | INTEGER | Car identifier (primary key) |
| model | TEXT | Car model name |
| license_plate | TEXT | License plate |
bridges
| column | type | meaning |
|---|
| bridge_id | INTEGER | Bridge identifier (primary key) |
| length_meters | INTEGER | Bridge length in meters, always positive |
bridge_crossings
| column | type | meaning |
|---|
| car_id | INTEGER | The car that crossed (references cars.car_id) |
| bridge_id | INTEGER | The bridge crossed (references bridges.bridge_id) |
| entry_time | TIMESTAMP | When the car entered the bridge |
| exit_time | TIMESTAMP | When the car left the bridge, NULL if no exit was recorded |
Each row of bridge_crossings is one crossing, identified by (car_id, bridge_id, entry_time).
Output Contract
Return these columns, in this order:
| column | meaning |
|---|
| car_id | The car that made the fastest crossing |
| model | That car's model |
| license_plate | That car's license plate |
| travel_time_seconds | Whole seconds from entry_time to exit_time, as an integer |
| speed_kmh | Speed in km/h, rounded to 2 decimal places |
Rank the qualifying crossings by unrounded speed descending, then by car_id ascending, bridge_id ascending and entry_time ascending, and return only the first row. If no crossing qualifies, return no rows.
Example
cars
| car_id | model | license_plate |
|---|
| 1 | Model 3 | ABC-123 |
| 2 | Civic | XYZ-789 |
| 3 | Corolla | JKL-456 |
bridges
| bridge_id | length_meters |
|---|
| 10 | 500 |
| 20 | 1200 |
bridge_crossings
| car_id | bridge_id | entry_time | exit_time |
|---|
| 1 | 10 | 2024-05-01 08:00:00 | 2024-05-01 08:00:20 |
| 2 | 20 | 2024-05-01 08:05:00 | 2024-05-01 08:05:37 |
| 3 | 10 | 2024-05-01 08:10:00 | NULL |
| 1 | 20 | 2024-05-01 09:00:00 | 2024-05-01 09:01:12 |
| 3 | 20 | 2024-05-01 09:30:00 | 2024-05-01 09:30:45 |
Expected result:
| car_id | model | license_plate | travel_time_seconds | speed_kmh |
|---|
| 2 | Civic | XYZ-789 | 37 | 116.76 |
The four completed crossings run at 90.00, 116.76, 60.00 and 96.00 km/h. Car 3's crossing on bridge 10 has no exit time, so it is left out.
Constraints and Clarifications
-
cars
has at most 10,000 rows,
bridges
at most 100 rows and
bridge_crossings
at most 100,000 rows.
-
length_meters
is always positive.
-
When
exit_time
is not NULL,
entry_time
is before
exit_time
. Timestamps have whole-second precision, so a qualifying travel time is a whole number of seconds.
-
A crossing with a NULL
exit_time
is incomplete and must be excluded.
-
A car can cross the same bridge many times. Each row is its own crossing, and the answer is one crossing, so do not combine rows per car.
-
Every
car_id
and
bridge_id
in
bridge_crossings
exists in
cars
and
bridges
.
-
Round only the reported
speed_kmh
. Ties are decided on the unrounded speed and then by the rule in the Output Contract.