Rank Carriers by On-Time Arrival Within Each Trade Lane
Last updated: November 29, 2026
Quick Overview
Using a shipments table with carrier, origin port, destination port, scheduled arrival and actual arrival, compute each carrier's monthly on-time arrival rate per trade lane (on time meaning within one day of schedule) and rank carriers within each lane and month.
Flexport
November 29, 20260
0
0 solved
Using a shipments table with carrier, origin port, destination port, scheduled arrival and actual arrival, compute each carrier's monthly on-time arrival rate per trade lane (on time meaning within one day of schedule) and rank carriers within each lane and month.
Schedule reliability is a key input when Flexport recommends a carrier to a shipper on a lane. The interviewer checks careful definitions (what counts as on time, what to do with shipments still at sea), aggregation by the right grain, window functions, and awareness of small-sample noise.
What the Interviewer Expects
- Define on time and the month a shipment belongs to before writing SQL
- Exclude or separately count shipments that have not arrived
- Aggregate at lane, month and carrier grain
- Use a window function for ranking within lane and month
- Apply a minimum volume threshold and discuss why
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 adjust the ranking so a carrier with 3 of 3 on time does not beat one with 95 of 100?
- Should the month be based on scheduled or actual arrival, and does it change the answer?
- How would you measure early arrivals, which also cause problems at the terminal?
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
shipments(shipment_id, carrier, origin_port, dest_port, scheduled_arrival, actual_arrival). Questions I would confirm: is scheduled_arrival the original schedule at booking or the latest carrier e...
Approach
- Filter to arrived shipments and compute an
on_time0/1 flag. - Group by lane, month and carrier;
AVG(on_time)is the rate. - Drop thin groups with a
HAVINGthreshold. RANK()partitione...