Weekly Unsafe Event Rate per Thousand Miles by Driver

Last updated: November 29, 2026

Quick Overview

Using trips and AI safety events tables, write SQL that computes each driver's weekly unsafe events per 1,000 miles for drivers with at least 500 miles that week, and ranks drivers from worst to best within each week.

Motive
Data Manipulation (SQL/Python)
Data Scientist
Motive
November 29, 2026
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Using trips and AI safety events tables, write SQL that computes each driver's weekly unsafe events per 1,000 miles for drivers with at least 500 miles that week, and ranks drivers from worst to best within each week.

Safety scores in Motive are normalized by exposure so a long-haul driver is not penalized just for driving more, and fleet managers use the ranking to decide who gets coaching. The interviewer checks for the classic join fan-out bug, correct normalization, and sensible exposure thresholds.

What the Interviewer Expects
  • Aggregate miles and events separately before joining to avoid fan-out
  • Keep drivers with zero events via a left join and coalesce
  • Normalize by miles and apply a minimum exposure threshold
  • Use a ranking function partitioned by week and explain rank versus row_number ties
  • Filter event types to behaviors that are actually unsafe
Key Topics to Cover
aggregation
fan-out
rate normalization
window functions
safety score
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you weight phone use more heavily than a hard brake?
  • Why might ranking on raw rates still be unfair for drivers near 500 miles, and how would you shrink the estimates?
  • How should events that were later dismissed in review be handled?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

trips(trip_id, driver_id, start_time, miles) and safety_events(event_id, driver_id, event_time, event_type). I would ask whether events can be dismissed by a reviewer (then filter on a status colu...

Approach

The trap is joining trips to events on driver and week and then summing miles, which multiplies miles by the number of events. Aggregate each table to driver and week first, then left join events onto...


Submit Your Answer
Markdown supported

Related Questions