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
November 29, 20260
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
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 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 ProblemsSample 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
- Order each car's events by time and use
LEADto look at the next event's type, yard, and timestamp. - A completed visit is an ARRIVE row whose next row is a DEPART at the same yard.
- Dwell in ...