Rolling Seven-Day Completed Trips per City With Gaps Filled
Last updated: November 29, 2026
Quick Overview
Given a trips table in pandas, compute daily completed trips per city, a trailing seven-day total and average, and the week-over-week change, making sure days with zero trips are included.
Uber
November 29, 20260
0
0 solved
Given a trips table in pandas, compute daily completed trips per city, a trailing seven-day total and average, and the week-over-week change, making sure days with zero trips are included.
City-level daily trip trends are a constant view for Uber ops and data science, and rolling windows over sparse daily data are a frequent source of subtle errors. The interviewer checks the zero-fill step and correct grouping in rolling calculations.
What the Interviewer Expects
- Filter to completed trips and aggregate by city and day
- Reindex to a full city by day grid so missing days are zero
- Compute the rolling sum within each city
- Use min_periods to avoid partial windows
- Compute week-over-week change on the rolling total
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
- Why is a time-based rolling window ('7D') different from rolling(7) on unfilled data?
- How would you flag anomalous days?
- How would you handle cities launched mid-period?
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
trips(trip_id, city, completed_at, status). Report window start to end. Rolling total covers the current day and the six before it. Cities come from the trips table (in production, from a city d...
Approach
- Filter completed, normalize timestamps to days, count per (city, day).
- Build a MultiIndex of every city crossed with every day, reindex with zeros.
- Group by city, rolling 7 with `min_periods=...