Find Members With Statin Therapy Gaps Longer Than Thirty Days
Last updated: November 29, 2026
Quick Overview
Given statin fills (member_id, fill_date, days_supply), merge overlapping or touching supply periods into continuous therapy episodes per member, then list every gap between episodes longer than 30 days with its start date and length.
CVS Health
November 29, 20260
0
0 solved
Given statin fills (member_id, fill_date, days_supply), merge overlapping or touching supply periods into continuous therapy episodes per member, then list every gap between episodes longer than 30 days with its start date and length.
CVS pharmacists and Aetna care managers run outreach to members who stop taking statins, since statin adherence is a Star Ratings measure and a cardiovascular risk. This is a classic gaps and islands problem in SQL, and the interviewer wants the running max trick rather than a cursor.
What the Interviewer Expects
- Computes each fill's coverage end date
- Uses a running MAX of prior end dates to detect episode breaks
- Builds episode ids with a cumulative SUM of break flags
- Measures gaps between consecutive episodes
- Explains why simple LAG of the previous fill fails
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 treat early refills that stockpile supply, as PDC does?
- How would you exclude gaps that overlap an inpatient admission?
- How would you turn this into a daily trigger for pharmacist outreach?
- What is the complexity of this approach on billions of rows?
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
statin_fills(member_id, fill_date DATE, days_supply INT). A fill covers [fill_date, fill_date + days_supply). Fills that overlap or start the day the previous supply ends belong to the same episod...
Approach
The trap: comparing only to the immediately previous fill fails when an earlier long fill (a 90 day supply) still covers today even though the previous short fill ended. So compute a running MAX of `e...