Days Between a Shops Buyer's First and Second Purchase
Last updated: November 29, 2026
Quick Overview
For buyers on Facebook and Instagram Shops, compute the number of days between their first and second purchase, then report the median gap by the country of the first order. Buyers with only one purchase are excluded from the gap but counted separately.
Meta
November 29, 20260
0
0 solved
For buyers on Facebook and Instagram Shops, compute the number of days between their first and second purchase, then report the median gap by the country of the first order. Buyers with only one purchase are excluded from the gap but counted separately.
Repeat purchase is a key health metric for commerce surfaces, and Meta interviewers like it because it requires picking the nth row per user cleanly. They watch for ROW_NUMBER with a tie-break and for not silently dropping one-time buyers from the narrative.
What the Interviewer Expects
- Numbers each buyer's orders chronologically with ROW_NUMBER
- Brings the second order's date onto the first order's row with LEAD or a pivot
- Computes day differences on dates, not timestamps, as specified
- Reports how many buyers never made a second purchase
- Uses a median rather than a mean for a skewed gap
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 compute the share of buyers who make a second purchase within 30 days, by first-order month cohort?
- Should refunded orders count as purchases? How would you exclude them?
- How would you compare gap distributions between buyers acquired via ads and organic buyers?
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: shop_orders(order_id, buyer_id, ordered_at, country, status). I will count only status = 'completed'. Two orders at the same second are possible from multi-shop checkouts; I will break ties...
Approach
Number orders per buyer by time. On the rank-1 row, LEAD(ordered_at) gives the second order's time (or NULL). Compute the date difference, then aggregate by first-order country with `PERCENTILE_CONT...