Find the fastest bridge crossing and its car from IoT traffic logs

Quick Overview

Write a PostgreSQL query that joins cars, bridges and sensor crossing logs to find the single fastest completed bridge crossing, then report the car, its travel time and its speed in km/h. It tests timestamp arithmetic, NULL filtering, safe numeric division and deterministic top-1 ordering.

Find the fastest bridge crossing and its car from IoT traffic logs

Company: NVIDIA

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

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. ```hint Watch the types Check what PostgreSQL does when it divides one integer by another, and where it places NULL values in a descending sort. ``` ### 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.

Overview: Write a PostgreSQL query that joins cars, bridges and sensor crossing logs to find the single fastest completed bridge crossing, then report the car, its travel time and its speed in km/h. It tests timestamp arithmetic, NULL filtering, safe numeric division and deterministic top-1 ordering.

NVIDIA logo
NVIDIA
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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

columntypemeaning
car_idINTEGERCar identifier (primary key)
modelTEXTCar model name
license_plateTEXTLicense plate

bridges

columntypemeaning
bridge_idINTEGERBridge identifier (primary key)
length_metersINTEGERBridge length in meters, always positive

bridge_crossings

columntypemeaning
car_idINTEGERThe car that crossed (references cars.car_id)
bridge_idINTEGERThe bridge crossed (references bridges.bridge_id)
entry_timeTIMESTAMPWhen the car entered the bridge
exit_timeTIMESTAMPWhen 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:

columnmeaning
car_idThe car that made the fastest crossing
modelThat car's model
license_plateThat car's license plate
travel_time_secondsWhole seconds from entry_time to exit_time, as an integer
speed_kmhSpeed 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_idmodellicense_plate
1Model 3ABC-123
2CivicXYZ-789
3CorollaJKL-456

bridges

bridge_idlength_meters
10500
201200

bridge_crossings

car_idbridge_identry_timeexit_time
1102024-05-01 08:00:002024-05-01 08:00:20
2202024-05-01 08:05:002024-05-01 08:05:37
3102024-05-01 08:10:00NULL
1202024-05-01 09:00:002024-05-01 09:01:12
3202024-05-01 09:30:002024-05-01 09:30:45

Expected result:

car_idmodellicense_platetravel_time_secondsspeed_kmh
2CivicXYZ-78937116.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.