Compute Terminal Dwell per Yard From Car Event Logs

Last updated: November 29, 2026

Quick Overview

Given a car_events table of (car_id, yard_id, event_type, event_ts) with ARRIVE and DEPART events, compute for each yard the number of completed visits, average dwell hours, and maximum dwell hours.

Bnsf
Data Manipulation (SQL/Python)
Data Engineer
Bnsf
November 29, 2026
Data Engineer
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Given a car_events table of (car_id, yard_id, event_type, event_ts) with ARRIVE and DEPART events, compute for each yard the number of completed visits, average dwell hours, and maximum dwell hours.

Terminal dwell is one of the headline service metrics a Class I railroad reports every week, and BNSF analytics teams build it from raw car movement events. The interviewer checks window-function pairing of arrivals and departures, how you handle missing or out-of-sequence events, and whether you exclude open visits.

What the Interviewer Expects
  • Pairs each ARRIVE with the next event of the same car using LEAD
  • Requires the next event to be a DEPART at the same yard
  • Excludes cars still in the yard from completed dwell
  • Converts timestamps to hours correctly
  • Discusses median versus mean for a skewed metric
Key Topics to Cover
Window functions
LEAD
Event pairing
Data quality
Operational metrics
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 report median dwell, and why might it be better than the mean?
  • How would you include cars currently sitting in the yard in a live dashboard?
  • What if a reader misses the DEPART event and the next event is an ARRIVE at another yard?
  • Industry dwell definitions exclude some cars, such as cars that run through on the same train. How would you implement that?
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

Each row is one event per car. I ask: can events arrive late or duplicated (assume deduped upstream), is the clock in one time zone (assume UTC), and what counts as a visit (an ARRIVE followed by a DE...

Approach
  1. Order each car's events by time and use LEAD to look at the next event's type, yard, and timestamp.
  2. A completed visit is an ARRIVE row whose next row is a DEPART at the same yard.
  3. Dwell in ...

Submit Your Answer
Markdown supported

Related Questions