Detect Subscribe and Save Plans With Consecutive Skipped Deliveries
Last updated: November 29, 2026
Quick Overview
Given sns_deliveries (subscription_id, customer_id, scheduled_date, outcome) where outcome is SHIPPED or SKIPPED for each scheduled Subscribe and Save delivery, return every run of two or more consecutive skipped deliveries per subscription with its start, end and length.
Amazon
November 29, 20260
0
0 solved
Given sns_deliveries (subscription_id, customer_id, scheduled_date, outcome) where outcome is SHIPPED or SKIPPED for each scheduled Subscribe and Save delivery, return every run of two or more consecutive skipped deliveries per subscription with its start, end and length.
Subscribe and Save retention analysts treat repeated skips as an early churn signal and use them to trigger re-engagement offers. Onsite SQL rounds at Amazon like this gaps-and-islands problem because it separates candidates who know the row-number difference trick from those who try to chain LAG calls.
What the Interviewer Expects
- Recognize the gaps-and-islands pattern
- Use the difference of two ROW_NUMBERs to label consecutive runs of the same outcome
- Define consecutive by schedule order rather than by fixed calendar spacing
- Return run boundaries and length, not just a yes or no flag
- Explain why chaining LAG only works for a fixed run length
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 return only subscriptions whose most recent run is still ongoing?
- How would you compute the probability of cancelling within 60 days after a two-skip run?
- What changes if a customer can change the delivery frequency mid-subscription?
- How would you handle deliveries that are still pending with no outcome yet?
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
- One row per scheduled delivery per subscription with
outcomein {SHIPPED, SKIPPED}. Pending deliveries are excluded upstream. - "Consecutive" means adjacent in the subscription's schedule, regardl...
Approach
Classic gaps and islands. Number all deliveries per subscription (seq) and, separately, number deliveries per subscription and outcome (seq_in_outcome). Within a run of identical outcomes both cou...