Flag ASIN Daily Sales Spikes Against a Trailing Baseline in Pandas
Last updated: November 29, 2026
Quick Overview
Given a dense daily table of units sold per ASIN (asin, date, units), use pandas to flag days whose units deviate by more than three standard deviations from the mean of the previous 28 days for that ASIN, excluding the current day from its own baseline and skipping ASINs without enough history.
Amazon
November 29, 20260
0
0 solved
Given a dense daily table of units sold per ASIN (asin, date, units), use pandas to flag days whose units deviate by more than three standard deviations from the mean of the previous 28 days for that ASIN, excluding the current day from its own baseline and skipping ASINs without enough history.
Retail and supply chain teams at Amazon watch for sudden demand spikes (a viral product, a pricing error, a bot) because they drain inventory and distort forecasts. This screen tests per-group rolling windows, the leakage trap of including today in its own baseline, and handling flat series where the standard deviation is zero.
What the Interviewer Expects
- Sort within each ASIN and compute rolling stats per group
- Shift by one day so the current value is excluded from the baseline
- Use min_periods so new ASINs are not flagged on thin history
- Guard against division by zero for perfectly flat series
- Explain why a dense daily series (zeros included) matters for the window
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
- Weekly seasonality makes Saturdays look like spikes; how would you adjust?
- Why might a median and MAD baseline be better than mean and standard deviation?
- How would you suppress repeated alerts during a multi-day promotion like Prime Day?
- How would you vectorize the rolling step without groupby transform lambdas?
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 ASIN per day with zeros filled; if the table were sparse, I would densify first so a 28 row window means 28 days.
- Baseline: mean and standard deviation of the previous 28 days, current...
Approach
- Sort by ASIN and date.
groupby("asin").shift(1)moves each value one day later within its ASIN, so the rolling window at day t covers t-28 to t-1. Without the shift, a spike inflates its own m...