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
Data Manipulation (SQL/Python)
Data Scientist
Amazon
November 29, 2026
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

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
LAG
PERCENTILE_CONT
segmentation
purchase cadence
deduplication
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. 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 Problems
Sample 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
  1. Distinct (customer_id, order_date).
  2. LAG(order_date) per customer gives the previous purchase day; the difference is the gap. The first purchase has no gap and is excluded.
  3. Segment per cus...

Submit Your Answer
Markdown supported