SQL for Rolling On-Time Rate Regressions by Delivery Hub
Last updated: November 29, 2026
Quick Overview
Given a deliveries table with hub, promised time, delivered time and status, compute each hub's trailing 7-day on-time delivery rate per day and flag days where it fell more than 5 points below the prior 7-day window.
Zipline
November 29, 20260
0
0 solved
Given a deliveries table with hub, promised time, delivered time and status, compute each hub's trailing 7-day on-time delivery rate per day and flag days where it fell more than 5 points below the prior 7-day window.
On-time delivery against the promised time is a headline operational metric for Zipline hubs. The interviewer checks careful metric definitions (what counts in the denominator), correct window frames, and awareness of sparse-day pitfalls.
What the Interviewer Expects
- Define on-time and the denominator explicitly
- Aggregate to hub-day before windowing
- Use correct ROWS frames for current and previous windows
- Compute ratios from summed counts, not averages of daily rates
- Guard against tiny denominators
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
- Some hubs have days with zero deliveries; how do you make the window cover calendar days?
- How would you attribute the regression to weather versus operational issues?
- How would you turn the flag into an alert without paging on noise?
- How would you compute the same metric per promised-hour bucket?
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
deliveries(delivery_id, hub_id, promised_at, delivered_at, status) with status in delivered, failed, cancelled_by_customer, cancelled_weather. Decisions to confirm: on-time means delivered with `del...
Approach
- Aggregate to one row per hub and day with on-time and eligible counts.
- Use two window frames per hub: the current 7 rows and the 7 rows before that.
- Divide summed counts to get rates, which w...