Rolling Seven Day Prescription Volume With Zero Fill Days
Last updated: November 29, 2026
Quick Overview
Given a DataFrame of prescription fills (store_id, fill_date), compute daily fill counts per store with days that had no fills filled in as zero, then add a trailing seven day average that is only reported once a full week of history exists.
CVS Health
November 29, 20260
0
0 solved
Given a DataFrame of prescription fills (store_id, fill_date), compute daily fill counts per store with days that had no fills filled in as zero, then add a trailing seven day average that is only reported once a full week of history exists.
Pharmacy workload forecasting at CVS depends on clean daily series per store, and missing days (holiday closures, data gaps) silently inflate rolling averages if not handled. Interviewers check whether the candidate notices that rolling over rows is not rolling over days.
What the Interviewer Expects
- Aggregates to daily counts per store
- Reindexes to a complete daily calendar with zeros
- Uses a 7 row window only after reindexing (or a time-based window)
- Sets min_periods so partial windows are not reported
- Explains when zero fill is wrong (store closed vs no data)
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 distinguish a closed store day from a data outage?
- How would you do this with rolling('7D') without reindexing?
- How would you vectorize this across 9,000 stores without a Python loop?
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
Input has one row per fill with a date (possibly with time). Output: store_id, date, rx_count, rx_7d_avg. A missing day means zero fills, which I would confirm, because a store closure should arguab...
Approach
Count per store and day. For each store, reindex to a full date_range from its first to last day with zeros. Then rolling(7, min_periods=7).mean(), which is now a true seven calendar day window be...