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.

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 tables `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 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. ### 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 ranking rule in the Output section.

Tables

cars(car_id INTEGER, model TEXT, license_plate TEXT)

bridges(bridge_id INTEGER, length_meters INTEGER)

bridge_crossings(car_id INTEGER, bridge_id INTEGER, entry_time TIMESTAMP, exit_time TIMESTAMP)

Hints

  1. Check what PostgreSQL does when it divides one integer by another, and where it places NULL values in a descending sort.
  2. Each row of bridge_crossings is its own crossing. The answer is one of those rows, so keep rows for the same car separate.
NVIDIAData ScientisteasyData Manipulation (SQL/Python)

Fastest Bridge Crossing

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 tables

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

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.

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 ranking rule in the Output section.

Expected Output

Tables

carsbridgesbridge_crossings
cars
ColumnType
car_idINTEGER
modelTEXT
license_plateTEXT
bridges
ColumnType
bridge_idINTEGER
length_metersINTEGER
bridge_crossings
ColumnType
car_idINTEGER
bridge_idINTEGER
entry_timeTIMESTAMP
exit_timeTIMESTAMP

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions