Cap Outlier Ad Revenue per Country at the 99th Percentile
Last updated: November 29, 2026
Quick Overview
Given per-user ad revenue for a week with a country column, winsorize revenue at the 99th percentile within each country (not globally), then report per country the raw mean, the capped mean, and how many users were capped.
Meta
November 29, 20260
0
0 solved
Given per-user ad revenue for a week with a country column, winsorize revenue at the 99th percentile within each country (not globally), then report per country the raw mean, the capped mean, and how many users were capped.
Revenue metrics in Meta experiments are heavy-tailed, and a few large advertisers' audiences or bots can swamp a mean. Interviewers use this to test groupby transform with quantile, clipping with a per-row threshold, and the judgment that caps should be computed per segment and fixed before looking at treatment effects.
What the Interviewer Expects
- Compute a per-country 99th percentile with groupby transform
- Clip each value to its own country's threshold, vectorized
- Report how many users were affected by the cap
- Compare raw and capped means to show the effect
- Explain why the threshold should be computed on pooled arms in an experiment
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
- Why might you prefer a log transform or CUPED over winsorization?
- How does winsorization change the variance and the power of a test?
- How would you pick the percentile for a metric with many zeros?
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
rev(user_id, country, revenue), one row per user, revenue nonnegative and many zeros. Capping only the upper tail. Pandas quantile uses linear interpolation by default, which is fine as long as it...
Approach
cap = groupby('country').revenue.transform(lambda s: s.quantile(0.99)) aligns a threshold to each row. Then revenue.clip(upper=cap) works element-wise because clip accepts a Series of bounds. Ag...