Find Locomotives Overdue for 92-Day Periodic Inspection
Last updated: November 29, 2026
Quick Overview
Given a locomotives table with a status column and an inspections table of (loco_id, inspected_on, inspection_type), list every in-service locomotive whose most recent periodic inspection is more than 92 days before an as-of date, including units that have never had one.
Bnsf
November 29, 20260
0
0 solved
Given a locomotives table with a status column and an inspections table of (loco_id, inspected_on, inspection_type), list every in-service locomotive whose most recent periodic inspection is more than 92 days before an as-of date, including units that have never had one.
A SQL screen of the kind BNSF data teams use, tied to federal rules that require a periodic locomotive inspection at least every 92 days. The interviewer checks the LEFT JOIN for never-inspected units, filtering to the right inspection type before aggregating, and careful date math.
What the Interviewer Expects
- Filters to the periodic inspection type before taking MAX
- Uses a LEFT JOIN so never-inspected units appear
- Restricts to in-service units only
- Computes days since inspection against a parameterized as-of date
- Orders output so the riskiest units come first
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you also flag units that will become overdue in the next 7 days?
- What if inspections can be recorded at a shop in a different time zone than the as-of date?
- How would you turn this into a daily alert without re-scanning all history?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
locomotives(loco_id, status) and inspections(loco_id, inspected_on, inspection_type). Questions I ask: does a daily inspection reset the clock (no, only PERIODIC_92 does), are stored units exemp...
Approach
- Aggregate the latest periodic inspection per unit in a CTE, filtering type before the MAX so a recent daily inspection cannot hide an overdue periodic one.
- LEFT JOIN from the fleet so units ...