Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Trips with Safety Interventions

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL Trips with Safety Interventions. Start with the requirements and the two tables on the right.

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.

Problem

Waymo analyzes autonomous vehicle trip records to identify sequences that may require operational review. Write a PostgreSQL query to find consecutive trips for the same vehicle where both trips recorded at least one safety intervention and the gap between them was no more than 30 minutes.

Requirements

  1. Count only interventions with intervention_type = 'safety_intervention' during each trip.
  2. Compare each trip with the immediately preceding trip for the same vehicle using a window function.
  3. Return adjacent trip pairs whose preceding and current trips both have safety interventions and whose gap is between 0 and 30 minutes, inclusive.
  4. Sort by vehicle_id and the current trip's start time.

Schema

trips
ColumnTypeDescription
trip_idPKINTUnique trip identifier
vehicle_idVARCHAR(20)Waymo vehicle identifier
start_timeTIMESTAMPTrip start timestamp
end_timeTIMESTAMPTrip end timestamp
safety_interventions
ColumnTypeDescription
intervention_idPKINTUnique intervention identifier
vehicle_idVARCHAR(20)Vehicle reporting the intervention
trip_idINTAssociated trip when available
occurred_atTIMESTAMPIntervention timestamp
intervention_typeVARCHAR(40)Intervention classification
Tablestripssafety_interventions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results