Flag Anomalous Days in Messenger Sends with a Trailing Z-Score
Last updated: November 29, 2026
Quick Overview
Given daily Messenger message sends per country, flag days whose value deviates from the trailing 28-day mean by more than 3 trailing standard deviations, where the trailing window excludes the current day and needs at least 14 prior days.
Meta
November 29, 20260
0
0 solved
Given daily Messenger message sends per country, flag days whose value deviates from the trailing 28-day mean by more than 3 trailing standard deviations, where the trailing window excludes the current day and needs at least 14 prior days.
Meta's metric monitoring flags sudden drops (an outage in one country, a logging regression after an app release) before they reach a weekly review. In an onsite data round this checks rolling statistics per group, excluding the current point to avoid self-masking, and minimum history requirements.
What the Interviewer Expects
- Compute rolling mean and std per country with groupby
- Shift by one so the current day is not in its own baseline
- Use min_periods to avoid flags from tiny windows
- Guard against zero standard deviation
- Return the z-score and direction, not just a boolean
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 handle weekly seasonality (weekends are always lower)?
- How would you avoid one anomaly inflating the std and masking the next week of anomalies?
- How would you set the threshold to control false alarms across 200 countries?
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
sends(country, date, sends), one row per country and day with no gaps (I would densify first if there were gaps). Window: the 28 days before the current one, minimum 14. Output adds baseline, sd...
Approach
Sort by country and date. Per country, sends.shift(1).rolling(28, min_periods=14) gives mean and std of strictly prior days. Use groupby().transform with a small function so the shift and rolling ...