Walk me through how you'd design and analyze an A/B test
Written by the mlprep team · Updated
You want to test whether a new recommendation algorithm increases click-through rate. Walk me through the full A/B testing process — from design through analysis. What decisions do you make upfront, and why?
A/B testing is hypothesis testing applied to product decisions. You split users randomly into control (old algorithm) and treatment (new algorithm), measure the difference, and determine whether it's real or noise. The rigor comes from the decisions you make before collecting any data.
Step 1: Define the hypothesis and metric#
- Primary metric: click-through rate (CTR) — clicks / impressions per user
- Null hypothesis H₀: CTR_treatment = CTR_control (the new algorithm has no effect)
- Alternative H₁: CTR_treatment ≠ CTR_control (two-sided test)
Why two-sided? The new algorithm might also decrease CTR. You need to detect harm too.
Pick one primary metric upfront. If you measure 20 metrics and pick the best one, you're p-hacking. Secondary metrics (revenue, session length) are monitored but not the decision criterion.
Step 2: Determine sample size#
You need enough users to detect a meaningful effect. Four inputs:
- Baseline rate: current CTR (say 5%)
- Minimum detectable effect (MDE): smallest improvement worth shipping (say 0.5% absolute, from 5.0% to 5.5%)
- Significance level α: false positive rate (typically 0.05)
- Power (1-β): probability of detecting the effect if it exists (typically 0.80)
For proportions:
n = (Z_α/2 + Z_β)² × (p₁(1-p₁) + p₂(1-p₂)) / (p₂ - p₁)²
With baseline 5%, MDE 0.5%, α=0.05, power=0.80:
- n ≈ 30,000 users per group (60,000 total)
This means you cannot peek at results and stop early when you see p < 0.05. The sample size is fixed upfront to control error rates.
Step 3: Randomization#
- Randomization unit: user (not session, not page view). If you randomize by page view, a single user sees both variants — contamination.
- Assignment method: hash(user_id + experiment_id) → deterministic, reproducible, no database needed at serving time.
- Guardrails: verify that randomization is balanced (50/50 split ± expected noise), and that covariates (geography, platform, new vs. returning users) are balanced between groups.
Step 4: Run the experiment#
- Let it run until you hit the predetermined sample size
- Do NOT stop early because the result "looks significant" — that inflates your false positive rate
- Monitor for data quality issues (broken logging, assignment bugs) but not for statistical significance
Step 5: Analyze#
For CTR (a proportion), use a two-proportion z-test:
z = (p_treatment - p_control) / sqrt(p_pooled × (1 - p_pooled) × (1/n_t + 1/n_c))
If |z| > 1.96 (for α = 0.05), reject the null. Report:
- The observed difference (e.g., +0.4% absolute, +8% relative)
- The 95% confidence interval for the difference
- The p-value
Step 6: Make the decision#
- If significant and positive: ship the new algorithm
- If significant and negative: kill it
- If not significant: the effect (if any) is smaller than your MDE. Decide whether a smaller effect is still worth pursuing with a larger test, or accept the null.
Interview red flags#
- Starting the test before choosing the primary metric, minimum detectable effect, and sample size.
- Peeking daily and stopping the moment p < 0.05, which inflates the false positive rate unless you use a sequential method.
- Randomizing at a different unit than you analyze, for example randomizing users but computing per-pageview variance.
- Declaring a win on statistical significance alone without the effect size and confidence interval.
You said "don't peek at results early." But in practice, experiments cost money and time. What if you want to stop early when results are clearly significant or clearly hopeless? Is there a rigorous way to do that?
Yes — sequential testing methods let you check results at predetermined intervals while controlling false positive rates. The key insight: the problem with peeking is that each look gives you a chance to falsely reject the null. If you check daily for 30 days at α = 0.05, your actual false positive rate is ~14%, not 5%.
Approach 1: Bonferroni correction (simple but conservative)#
If you plan to check K times, use α/K at each check. For 5 checks: use α = 0.01 at each peek. This guarantees overall α ≤ 0.05 but is overly conservative — it reduces power significantly.
Approach 2: O'Brien-Fleming boundaries (standard in industry)#
Pre-specify check points (e.g., at 25%, 50%, 75%, 100% of planned sample). At each check, use a progressively easier significance threshold:
- At 25%: need z > 4.05 (nearly impossible — you almost never stop here)
- At 50%: need z > 2.86
- At 75%: need z > 2.34
- At 100%: need z > 2.02 (close to the standard 1.96)
The boundaries are calibrated so the overall false positive rate across all checks is exactly 0.05. Early stopping only happens for very strong effects — which is exactly right.
Approach 3: Always-valid confidence intervals (modern approach)#
Companies like Netflix and Optimizely use confidence sequences that are valid at any stopping time. You can check every day, every hour, or continuously — the confidence interval is always valid.
The tradeoff: always-valid intervals are wider than fixed-sample intervals. You need ~15-30% more samples to achieve the same power. But you get complete flexibility in when to stop.
Approach 4: Bayesian testing#
Compute the posterior probability that treatment > control. Stop when P(treatment > control) > 0.95 or < 0.05. No fixed sample size needed.
Tradeoff: requires specifying a prior (which you can set to be very weak), and the frequentist error rate guarantees are weaker. But in practice, for product decisions, "95% probability that treatment is better" is more actionable than "p < 0.05."
What industry actually does: most mature experimentation platforms use either O'Brien-Fleming or always-valid intervals. They let you check weekly and stop early for clear wins or losses, while maintaining statistical rigor.
Go deeper: A/B testing ML models covers the ML-specific side: metric choice, network interference, cluster randomization, and switchback experiments.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
A/B testing = rigorous hypothesis testing for product decisions. Decide sample size upfront based on minimum detectable effect and desired power. Don't peek at results early unless using sequential testing methods (O'Brien-Fleming, always-valid intervals) that control false positive rates. The analysis itself is straightforward — the discipline is in the design decisions that happen before data collection begins.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- Your A/B test shows a statistically significant 2% improvement in CTR, but revenue per user decreased by 1% (not significant). How do you make the launch decision?
- How do you handle network effects in an A/B test — for example, testing a social feature where one user's experience affects another's?
- What is the multiple comparisons problem, and how do you handle testing 5 variants simultaneously?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
Explain p-values, Type I/II errors, and statistical power to a non-statistician
What is the multiple testing problem and how do you control for it?
How do you determine the sample size needed for an A/B test?
How do you run an A/B test to evaluate a new ML model?