Sample size for a test

90 min6 partsCoreFundamentalsChecked Nov 2026
Start the lessonShort steps, each with something to do
What you'll learn
  • Say what alpha, beta and power each protect you from, and what each one costs in users
  • Size each arm for two rates with both arms' variances and the two-sided z, then round up
  • Tell an absolute lift from a relative one, and say why half the lift takes about four times the users
  • Say how the baseline rate moves the number of users, for a lift in points and for a lift in percent
  • Get a quick number from Lehr's rule, 16σ²/Δ², and say where the 16 comes from and how close it lands
  • Turn users into days, and say what uneven splits, CUPED and clustering do to the count
  • Say why a significant result from an underpowered test overstates the effect, and by how much
Part 1 of 6about 15 min
Two errors

How many users the test needs, and how many days

A prep site lists a Netflix data science onsite question, posted in October 2025, on designing and powering a frequency-cap experiment: "Design an experiment and provide power calculations that account for interference and clustering" (1 source). A candidate's report of a Netflix data science onsite, apparently the same one, says both technical cases were about ad frequency caps (1 report). Interference and clustering adjust the answer. The calculation they adjust comes first, and our version of it reads like this:

A sign-up flow converts 10% of visitors, and we want to detect a lift to 12%. Write sample_size_per_arm(p_baseline, mde, alpha=0.05, power=0.8, relative=False): the users each arm needs for a two-sided test. Then write days_to_run: how long the test runs, in whole weeks, when 1,500 new users arrive a day and half of them enter it.

Too few users, and a real improvement disappears into the noise. Too many, and you spend weeks of traffic that another test needed.

The practice comes in four levels. Level 1 sizes the test at two-sided alpha 0.05 and 80% power, with each arm's own variance, and rounds up to whole users. Level 2 takes any alpha and power. Level 3 takes a lift stated as a share of the baseline, with relative=True. Level 4 adds days_to_run, which turns users into days, in whole weeks.

From four choices to a number of daysInteractive
you choose = a 10% baseline, a lift to 12%, two-sided alpha 0.05, 80% power.

You don't write it yet. The lesson starts with the two ways a test can be wrong, builds the formula one piece at a time, then turns users into days and answers the follow-ups: uneven splits, CUPED, clusters and ratio metrics. At the end you solve the whole problem, level by level, in an interview simulation with a 30-minute timer.

This lesson builds on the A/B test lesson, which read a finished test with the two-proportion z-test. This one decides, before the test starts, how much data that test gets. The CUPED lesson comes next and gets the same answer from fewer users.

A test can be wrong in two ways, and you choose how often

A test can say significant when nothing changed, or miss a change that's real. Here are both, as a table.

No real effectA real effect the size of the MDE
Significantfalse positive, probability α\alphatrue positive, probability 1−β1 - \beta: the power
Not significantcorrect, probability 1−α1 - \alphafalse negative, probability β\beta

Each column is one state of the world. Each row is what the test says. You pick the two error rates before the test starts.

Predict

At alpha = 0.05, how many of twenty changes that do nothing still come out significant?

Pick an answer above to see the rest
Predict

A true 2-point lift, 3,839 users per arm. What share of tests land past the bar and come out significant?

Pick an answer above to see the rest

Lower alpha or higher power costs more users

You can tighten either error rate. Guess what the usual step costs.

Predict

Raising power from 80% to 90% costs about how many more users?

Pick an answer above to see the rest

Power is always power to detect one effect size

80% power is a promise about one true lift. Ask what happens at another.

Predict

A test sized for a 2-point lift meets a true 3-point lift. How often does it catch it?

Pick an answer above to see the rest

The minimum detectable effect is a decision you make

Alpha and power are settings. The third input, the lift to detect, is a judgment.

Predict

Who picks the minimum detectable effect?

Pick an answer above to see the rest
Part 1 done
Part 2 of 6about 22 min
The formula

The formula: how far the true lift must sit from zero

The formula comes from one picture: where the true lift has to sit for the test to catch it.

Predict

At two-sided alpha 0.05 and 80% power, how many standard errors must the true lift sit from zero?

Pick an answer above to see the rest

A two-sided test puts half of alpha in each tail

The 2.8 is two z's added. Here they are, from Python's normal curve.

from statistics import NormalDist

print(round(NormalDist().inv_cdf(0.975), 3))
print(round(NormalDist().inv_cdf(0.80), 3))
print(round(NormalDist().inv_cdf(0.90), 3))
Predict

inv_cdf(0.975) is 1.96. What's inv_cdf(0.80), the point with 80% of the curve below it?

Pick one to see what it prints.
Pick an answer above to see the rest

Each arm brings its own variance, and p(1 - p) sets how noisy its users are

The other half of the formula is the variance, and each arm brings its own.

Predict

Which conversion rate has the noisiest users?

Pick an answer above to see the rest

The formula gives a minimum, so round up

The formula's answer is rarely a whole number, and the way you round it matters.

Users per arm3,8383,838.103,839
Power79.999%exactly 80%just over 80%
Comes fromint(), round()the formulamath.ceil

The formula gives the smallest nn that reaches the target power, and users come whole. Anything below it falls short, so round up.

In the editor, z_sum and arm_variances are ready. Write sample_size_per_arm(p_baseline, mde, alpha=0.05, power=0.8) from them, rounded up.

CheckpointUsers per arm, rounded up

Finish sample_size_per_arm(p_baseline, mde, alpha=0.05, power=0.8) from the two pieces in the editor. It's the squared z's times both arms' variance, over the lift squared, returned as the whole number of users that reaches the target power.

Put together, the formula is one short function

Here are the pieces in one function, the shape level 1 of the practice asks for.

from math import ceil
from statistics import NormalDist


def sample_size_per_arm(p_baseline, mde, alpha=0.05, power=0.8):
    # absolute, not relative
    p_treat = p_baseline + mde
    # two-sided: 1.96
    z_alpha = NormalDist().inv_cdf(1 - alpha / 2)
    # 0.84 at 80% power
    z_power = NormalDist().inv_cdf(power)
    variance = p_baseline * (1 - p_baseline) + p_treat * (1 - p_treat)
    return ceil((z_alpha + z_power) ** 2 * variance / mde ** 2)


print(sample_size_per_arm(0.10, 0.02))
print(sample_size_per_arm(0.10, 0.01))
print(sample_size_per_arm(0.10, 0.02, power=0.9))
Predict

10% to 12% needs 3,839 per arm. About how many does a 1-point lift, 10% to 11%, need?

Pick one to see what it prints.
Pick an answer above to see the rest

What halving the lift costs

The lift is the input you have the most say over. Watch what halving it does to n.

for lift in [0.04, 0.02, 0.01, 0.005]:
    print(lift, sample_size_per_arm(0.10, lift))
Predict

Each time the lift halves, the users needed grow by about what factor?

Pick one to see what it prints.
Pick an answer above to see the rest

The baseline moves n, and which way depends on how the lift is stated

The baseline moves n too, and which way depends on how the lift is stated.

print("baseline   +2 points   +10% of it")
for p in [0.02, 0.10, 0.30, 0.50]:
    print(f"{p:>8.0%} {sample_size_per_arm(p, 0.02):>11,} {sample_size_per_arm(p, 0.10 * p):>12,}")
Predict

The lift is fixed at 2 points. Does a 30% baseline need more or fewer users than a 10% one?

Pick one to see what it prints.
Pick an answer above to see the rest
Part 2 done
Part 3 of 6about 8 min
Quick numbers

Lehr's rule gives a quick number: 16σ²/Δ² per arm

In an interview you want a number in ten seconds, before the exact one.

Predict

Lehr's rule is 16σ²/Δ². For 10% to 12%, σ² = 0.09 and Δ = 0.02. About how many per arm?

Pick an answer above to see the rest

With the baseline's variance, Lehr's rule runs low when the treatment arm is noisier

How close the quick number lands depends on which variance you plug in.

Baseline to treatmentExactLehr, baseline varianceLehr, average variance
10% to 12%3,8393,600 (6.2% low)3,912 (1.9% high)
10% to 11%14,74914,400 (2.4% low)15,032 (1.9% high)
2% to 2.4%21,10619,600 (7.1% low)21,512 (1.9% high)
30% to 32%8,3918,400 (0.1% high)8,552 (1.9% high)
50% to 52%9,80410,000 (2.0% high)9,992 (1.9% high)

With the baseline's variance alone, the rule has the same blind spot as the baseline-variance mistake. It runs low whenever the treatment arm is noisier.

With the two arms' average variance, it's always 1.9% high, because 16 is 15.70 rounded up.

In the editor, write lehr(p_baseline, mde, average=False): both versions of the quick number, to check the table.

Watch · 6 min

Emma Ding's interview take on the 16σ²/δ² rule of thumb and its four inputs, alpha, beta, the variance and delta.

Sample Size Estimation in A/B Testing: Easy Explanation for Data Science Interviews · Emma Ding · Jun 2022YouTube

A worked example

10% to 12% needs 3,839 users per arm, 7,678 in all

A sign-up flow converts 10% of visitors. The team wants to detect a lift to 12%, two-sided at α=0.05\alpha = 0.05, with 80% power.

StepValue
Control variance, 0.10×0.900.10 \times 0.900.0900
Treatment variance, 0.12×0.880.12 \times 0.880.1056
Sum0.1956
z0.975+z0.80z_{0.975} + z_{0.80}1.9600 + 0.8416 = 2.8016
Squared7.8489
7.8489×0.1956 / 0.0227.8489 \times 0.1956 \,/\, 0.02^23,838.10
Round up3,839 per arm, 7,678 in all

In simulation, 3,839 per arm catches the lift 80% of the time

Check it the way you'd check any function: simulate. Draw both arms' conversions many times, run the pooled z-test from the ab-test lesson on each draw, and count how often it comes out significant.

import numpy as np
from statistics import NormalDist


def simulate(n, p1, p2, reps=200_000, alpha=0.05, seed=0):
    rng = np.random.default_rng(seed)
    # A's conversions in each test
    a = rng.binomial(n, p1, size=reps)
    b = rng.binomial(n, p2, size=reps)
    diff = (b - a) / n
    pooled = (a + b) / (2 * n)
    # pooled z-test
    z = diff / np.sqrt(pooled * (1 - pooled) * 2 / n)
    significant = np.abs(z) > NormalDist().inv_cdf(1 - alpha / 2)
    return significant.mean(), diff[significant].mean()


share, _ = simulate(3839, 0.10, 0.12)
print(share)
share, _ = simulate(3839, 0.10, 0.10)
print(share)
Predict

3,839 per arm and a true 2-point lift, over 200,000 simulated tests. What share comes out significant?

Pick one to see what it prints.
Pick an answer above to see the rest
Part 3 done
Part 4 of 6about 14 min
Days and power

From users to days

The formula gives users. A schedule needs days, and the days come in whole weeks. days_for(users, users_per_day) is the function you write at the end of this step: the days to collect users, in whole weeks.

from math import ceil

# both arms, at 1,500 a day with half in the test
for n_per_arm in (3839, 5139, 14749):
    print(days_for(2 * n_per_arm, 1500 * 0.5))
Predict

1,500 new users a day, half of them in the test. How long does the 2-point test run?

Pick one to see what it prints.
Pick an answer above to see the rest

Count new users, not daily actives, and run whole weeks

Which daily count goes under the users matters as much as the rounding.

Predict

To turn users into days, which daily count do you use?

Pick an answer above to see the rest

An underpowered test overstates the effects it finds

Significance only lets the big estimates through

Run the worked example's simulation on a smaller truth. The test is still sized for 2 points, but the true lift is 1 point:

share, average = simulate(3839, 0.10, 0.11)
print(round(share, 3), round(average, 4))
Predict

The test is sized for 2 points, and the true lift is 1 point. What share of tests come out significant?

Pick one to see what it prints.
Pick an answer above to see the rest

The smaller the true lift, the more the significant estimates exaggerate it

Now shrink the truth further, and keep the test the same.

Predict

The test stays sized for 2 points while the true lift shrinks toward 0.25 points. What happens to the average significant estimate?

Pick an answer above to see the rest

Underpowered wins shrink when you rerun them, and misses kill good ideas

A launch reported at +1.8 points delivers +1. That's the winner's curse: on average, the winners you pick overstate their own effect.

Rerun the 1-point winners, and they average 1.00 point, and only 30% come out significant again. The misses mislead too: at 30% power, 70% of real 1-point lifts come out not significant, and a good idea dies.

The survey calls underpowered experiments a common mistake. The fix is the sizing in this lesson:

  • Power the test for an effect you believe, not one picked to make the test cheap.
  • Report the confidence interval with the estimate.
  • Rerun surprising wins.
Part 4 done
Part 5 of 6about 14 min
Fewer users

In an uneven split, the small arm sets the precision

Teams split traffic unevenly for real reasons, like ramping a risky change up slowly or keeping a small holdout. It costs time, because the small arm sets the precision. With kk treatment users per control user, the control needs

nC=(z1−α/2+z1−β)2Δ2(σC2+σT2k)n_C = \frac{(z_{1-\alpha/2} + z_{1-\beta})^2}{\Delta^2} \left(\sigma_C^2 + \frac{\sigma_T^2}{k}\right)

and the treatment gets nT=k nCn_T = k\,n_C, where σC2\sigma_C^2 and σT2\sigma_T^2 are each arm's p(1−p)p(1-p).

def sample_size_split(p_control, mde, treat_share, alpha=0.05, power=0.8):
    p_treat = p_control + mde
    z = NormalDist().inv_cdf(1 - alpha / 2) + NormalDist().inv_cdf(power)
    # treatment users per control user
    k = treat_share / (1 - treat_share)
    var_c, var_t = p_control * (1 - p_control), p_treat * (1 - p_treat)
    n_control = ceil(z ** 2 * (var_c + var_t / k) / mde ** 2)
    return n_control, ceil(k * n_control)


print("split  control  treatment    total  vs 50/50")
for treat_share in [0.5, 0.2, 0.1, 0.01]:
    control, treatment = sample_size_split(0.10, 0.02, treat_share)
    split = f"{round(100 * (1 - treat_share))}/{round(100 * treat_share)}"
    total = control + treatment
    print(f"{split:>5} {control:>8,} {treatment:>10,} {total:>8,} {total / 7678:>8.2f}x")
Predict

The same 10% to 12% test on a 90/10 split. About how many users in all, against 7,678 at 50/50?

Pick one to see what it prints.
Pick an answer above to see the rest

With one treatment 50/50 is all but optimal, and several treatments share a bigger control

Several treatments change the answer, because they share one control.

Predict

Three treatments share one control. How big should the control be, against each treatment?

Pick an answer above to see the rest

Less variance means fewer users, and CUPED cuts the variance

The next way to save users attacks the variance in the formula's top line.

Predict

A pre-experiment covariate correlates 0.7 with the metric. What share of the variance does CUPED keep?

Pick an answer above to see the rest

Triggering analyzes only the users the change can reach

Triggering works on the lift, not the variance.

Predict

Only 20% of users can be affected, and they go from 10% to 12%. Analyze everyone, and the lift becomes what?

Pick an answer above to see the rest

A steadier metric needs fewer users

Revenue per user has a heavy tail: a few big spenders swing the average. A conversion rate doesn't. In the survey's example, detecting a 5% change in conversion instead of in revenue cut the sample size by a factor of 3.3.

Part 5 done
Part 6 of 6about 17 min
Follow-ups

What interviewers ask next

The Netflix question from the first step comes back here, with every adjustment the lesson covered.

The frequency-cap question: start from the base n, then adjust it

The frequency-cap question, from the base nInteractive
base n = 3,839 per arm for 10% going to 12%.

A frequency cap limits how many times one person sees the same ad. The Netflix question a prep site lists asks for power calculations "that account for interference and clustering" (1 source). Each piece moves nn in a direction you can work out, so take them in the order of the figure.

The metric and the MDE. Measure per member, the unit you randomize, and state the MDE as two rates. A cap cuts impressions, so clicks per impression can rise even while clicks per member fall. If you need a per-impression rate, size it with the delta method, which a later step shows.

Who can be affected. A cap of three a day changes nothing for someone who would have seen two. Log in both arms whether each member would have hit the cap, and analyze those members. That's triggering, worth 4.7 times fewer users in its example.

Clustering and interference. The next two steps take these one at a time.

Then days. New members per day in, whole weeks out, as before.

Randomizing whole clusters multiplies n by the design effect

Randomize households or regions, and you randomize a unit that holds several of the units you measure. Outcomes inside one unit move together. Multiply nn by the design effect, DEFF, with mm members per cluster and intra-cluster correlation ρ\rho:

DEFF=1+(m−1) ρ\text{DEFF} = 1 + (m - 1)\,\rho

The intra-cluster correlation is the correlation between two members of the same cluster. It's 0 when the cluster says nothing about a member's outcome, and 1 when every member of a cluster has the same one.

Predict

Clusters of 20 members, with ρ = 0.05. What's the design effect, 1 + (m - 1)ρ?

Pick an answer above to see the rest

When the arms compete for one ad budget, control can change too

Ads add one more problem: the two arms can compete for the same budget.

When the arms compete for one ad budgetInteractive
one campaign = chasing its delivery goal.

Ads run against budgets and delivery goals. When the cap stops showing an ad to treatment members, a campaign chasing its goal can show it to control members instead. So control changes too, and the gap between arms isn't what a full launch would do.

The fix is to randomize at a level where the arms don't compete for the same impressions, such as whole markets. Or give each arm its own share of every campaign's budget. Both create clusters, so the design effect applies, and the number of clusters becomes the constraint.

A ratio metric needs its variance per member, not per impression

A frequency cap is often judged on a rate per impression, and that rate's variance needs care. ratio_variance is the function you write at the end of this step: the variance per member.

clicks = [0, 0, 1, 3]
impressions = [10, 10, 10, 10]
r = sum(clicks) / sum(impressions)
print(round(ratio_variance(clicks, impressions), 4))
print(round(r * (1 - r) / (sum(impressions) / 4), 4))
Predict

Four members, 10 ads each, one of them doing most of the clicking. Is the per-member variance bigger or smaller than the coin-flip view?

Pick one to see what it prints.
Pick an answer above to see the rest

With fixed traffic, solve for the smallest lift you can detect

When traffic is fixed, the question flips: We have two weeks. What can we detect? Solve for Δ\Delta. The exact answer comes from bisecting on the function you already wrote:

def smallest_lift(n_per_arm, p_baseline, alpha=0.05, power=0.8):
    lo, hi = 0.0, 1 - p_baseline
    # bisect: n falls as the lift grows
    for _ in range(60):
        mid = (lo + hi) / 2
        if sample_size_per_arm(p_baseline, mid, alpha, power) > n_per_arm:
            lo = mid
        else:
            hi = mid
    return hi


print(round(smallest_lift(5000, 0.10), 4))
Predict

5,000 users per arm on a 10% baseline, 80% power. What's the smallest lift you can detect?

Pick one to see what it prints.
Pick an answer above to see the rest

With a Bonferroni correction, three treatments need 1.33 times the users each

Several treatments also mean several comparisons, and each one is another chance for a false positive.

Predict

Three treatments with a Bonferroni correction. What alpha does each comparison run at?

Pick an answer above to see the rest

The n assumes you look once, at the end

In simulation, checking a no-effect test at the end of each of 14 days and stopping at the first significant result turned a 5% false positive rate into 22%. If you need to stop early, use a sequential design and plan the looks into the sizing. The ab-test lesson walks through those designs.

The practice problem, start to finish

That's every piece of the practice problem, and every follow-up it lists. The simulation asks for both functions under a timer, so rehearse the order once.

Check yourself
5 questions
  1. 1.You compute the alpha z as NormalDist().inv_cdf(1 - alpha). What happens to the 10% to 12% test?

  2. 2.A teammate sizes a test of 2% to 3% with 2 * 0.02 * 0.98 as the variance. What goes wrong?

  3. 3.The PM wants to detect a 2% lift on a 10% conversion rate and means 2% of the baseline. How many users per arm is that, against 3,839 for 2 points?

  4. 4.The formula gives 3,838.10 users per arm. What should the function return?

  5. 5.A test sized for a 2-point lift, 3,839 users per arm on a 10% baseline, runs on a change whose true lift is 1 point. When it comes out significant, what lift does it report on average?

Where this shows up

The reports behind this lesson, each with its source.

A prep site lists a Netflix DS onsite question (posted 2025-10-13): design and power a frequency-cap experiment that accounts for interference and clustering.

Prep site·PracHub (prep site question bank)·2025-10-13Source

Data scientist onsite (Oct 2025 report): both technical cases were about ad frequency caps.

Candidate report·PracHub (first-person report, curated and edited by the site)·2026-08-25Source

Practice

Sources (3)
  1. Controlled experiments on the web: survey and practical guide · Data Mining and Knowledge Discovery 18, 140-181 (2009) · Jul 30, 2008
    “where n is the number of users in each variant and the variants are assumed to be of equal size”
  2. Sixteen S-squared over D-squared: A relation for crude sample size estimates · Statistics in Medicine 11(8), 1099-1102 (PubMed record) · Jun 15, 1992
    “I suggest for memorization an equation for calculating approximate sample size requirements intended only for a specific set of values (80 per cent power for a two-tailed alpha = 0.05 test)”
  3. Beyond Power Calculations: Assessing Type S (Sign) and Type M (Magnitude) Errors · Perspectives on Psychological Science 9(6), 641-651 · Nov 17, 2014
    “But when power gets much below 0.5, the exaggeration ratio becomes high (that is, statistically significant estimates tend to be much larger in magnitude than true effect sizes).”