Compare Median Repurchase Intervals for Subscribe and Save Customers
Last updated: November 29, 2026
Quick Overview
For each customer, compute the days between consecutive order days, then report the median and 90th percentile gap for customers who use Subscribe and Save versus those who do not.
Amazon
November 29, 20260
0
0 solved
For each customer, compute the days between consecutive order days, then report the median and 90th percentile gap for customers who use Subscribe and Save versus those who do not.
A window function question in the style of Amazon consumables analytics, where Subscribe and Save is judged partly by how it changes purchase cadence. The interviewer checks LAG gap computation, collapsing same-day orders, and correct use of PERCENTILE_CONT as an aggregate.
What the Interviewer Expects
- Collapse orders to one row per customer per order day
- Compute gaps with LAG partitioned by customer
- Define the customer segment once at the customer level
- Use PERCENTILE_CONT WITHIN GROUP for median and p90
- Report the number of customers and gaps per segment
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 the median preferred over the mean here?
- How would you avoid the comparison being driven by who chooses Subscribe and Save?
- How would you look at gaps only within the same product category?
- How would you compute the interval just before and just after a customer enrolled?
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
Table orders(order_id, customer_id, order_ts, is_subscribe_save). Segment: a customer is a Subscribe and Save customer if any of their orders in the period is a subscription order. Same-day orders c...
Approach
- Distinct
(customer_id, order_date). LAG(order_date)per customer gives the previous purchase day; the difference is the gap. The first purchase has no gap and is excluded.- Segment per cus...