Find the Order Where Each Customer's Lifetime Spend Crossed Five Hundred Dollars
Last updated: November 29, 2026
Quick Overview
For each customer, identify the order at which cumulative lifetime spend first reached at least $500, and how many days after their first order that happened.
Amazon
November 29, 20260
0
0 solved
For each customer, identify the order at which cumulative lifetime spend first reached at least $500, and how many days after their first order that happened.
A running-total question in the style of Amazon customer analytics, used for milestones such as loyalty thresholds and high-value customer flags. The interviewer checks a correctly framed cumulative SUM, a deterministic tiebreak, and an exact definition of the crossing row.
What the Interviewer Expects
- Compute a running SUM partitioned by customer and ordered by time with an explicit frame
- Break timestamp ties deterministically with order_id
- Identify the crossing row as cumulative at least 500 and previous cumulative below 500
- Compute days from first order with a window MIN
- Exclude cancelled orders and state how refunds are treated
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 median days to $500 by signup cohort?
- What if refunds can push cumulative spend back below $500?
- How would you find customers who never crossed and how close they got?
- Why does the default window frame give wrong results with duplicate timestamps?
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, order_total, status). Assumptions: use orders with status = 'SHIPPED'; amounts are in USD; refunds are ignored for this milestone (I would raise that ...
Approach
- Running total per customer with
SUM(order_total) OVER (PARTITION BY customer_id ORDER BY order_ts, order_id ROWS UNBOUNDED PRECEDING). - The previous cumulative is
running_total - order_total,...