Welcome to the SQL screen.
The question is on your right: Autonomous vs Manual Drive Time. Read through the requirements and the two tables first.
Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?
You’re working on the data platform for a self-driving vehicle fleet operating in multiple US cities. The fleet generates a high-volume event stream (billions of rows/month) where each event records a vehicle’s current control state (e.g., autonomous vs manual). Safety and compliance teams use these logs to audit disengagements and to report how much time vehicles spend under autonomous control versus human control.
A common issue is that the event stream is state-change oriented: you only get a new row when the state is recorded (often on change, but sometimes periodically). To compute time-in-state, you must interpret each row as the start of an interval that ends at the next timestamp for the same vehicle.
Write a SQL query that calculates, for each vehicle and calendar day, the total time (in seconds) spent in autonomous mode versus manual mode.
vehicle_state_events as the start of a state interval.trip_end_ts from vehicle_trips.trip_start_ts), and return two columns: autonomous_seconds and manual_seconds.vehicle_id, trip_date.| Column | Type | Description |
|---|---|---|
| trip_idPK | BIGINT | Unique trip identifier |
| vehicle_id | BIGINT | Vehicle identifier |
| trip_start_ts | TIMESTAMP | Trip start timestamp in UTC |
| trip_end_ts | TIMESTAMP | Trip end timestamp in UTC |
| Column | Type | Description |
|---|---|---|
| event_idPK | BIGINT | Unique event identifier |
| trip_id | BIGINT | Trip identifier referencing vehicle_trips.trip_id |
| vehicle_id | BIGINT | Vehicle identifier (denormalized for performance) |
| event_ts | TIMESTAMP | Timestamp when the state became active (UTC) |
| vehicle_state | VARCHAR(32) | Control state: autonomous or manual |