Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Autonomous vs Manual Drive Time
00:00
5 left

Autonomous vs Manual Drive Time

HardSQL · PostgreSQL

Problem

Business Context

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.

Task

Write a SQL query that calculates, for each vehicle and calendar day, the total time (in seconds) spent in autonomous mode versus manual mode.

Requirements

  1. Consider each row in vehicle_state_events as the start of a state interval.
  2. The interval ends at the next event timestamp for the same vehicle.
  3. If there is no next event for the vehicle, end the interval at trip_end_ts from vehicle_trips.
  4. Split time by vehicle_id + trip_date (derived from trip_start_ts), and return two columns: autonomous_seconds and manual_seconds.
  5. Ignore any intervals where the computed end time is <= start time (bad data / duplicates).
  6. Output should include one row per vehicle_id, trip_date.

Schema

vehicle_trips
ColumnTypeDescription
trip_idPKBIGINTUnique trip identifier
vehicle_idBIGINTVehicle identifier
trip_start_tsTIMESTAMPTrip start timestamp in UTC
trip_end_tsTIMESTAMPTrip end timestamp in UTC
vehicle_state_events
ColumnTypeDescription
event_idPKBIGINTUnique event identifier
trip_idBIGINTTrip identifier referencing vehicle_trips.trip_id
vehicle_idBIGINTVehicle identifier (denormalized for performance)
event_tsTIMESTAMPTimestamp when the state became active (UTC)
vehicle_stateVARCHAR(32)Control state: autonomous or manual
Tablesvehicle_state_eventsvehicle_trips
Interviewer

Your question is Autonomous vs Manual Drive Time. Start with the requirements and the two tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.