KatInterview

Two-Sample Z-Test: Compute Minimum Experiment Size

Statistics & ExperimentsModel answer
HardModel answerStatistics & Experimentsstatisticsa/b testinghypothesis testing

Last updated

Given historical metric data and test parameters, derive the smallest total sample size that meets a specified significance level and statistical power.

Setting the Scene

You are building an experimentation toolkit. One core utility calculates how many users an A/B test needs before it starts, so the team commits to a sample size up front rather than peeking and stopping early.

What You Receive

ParameterTypeDescription
observed1-D array of floatsHistorical measurements of the metric you plan to test. Used to estimate outcome variability.
alphafloatTwo-sided significance level (e.g., 0.05).
powerfloatDesired probability of detecting a real effect (e.g., 0.8).
deltafloatSmallest absolute mean difference worth detecting (treatment minus control).

What You Must Return

A single integer: the minimum total number of participants across both groups combined.

Assumptions

  • The test is two-sided and uses the z-distribution (normal approximation).
  • Treatment and control groups are equal in size.
  • Both groups share the same outcome variance, estimated from observed.
  • Estimate the standard deviation using the sample formula (unbiased, ddof=1).

Requirements

  1. Estimate σ from observed.
  2. Apply the standard per-group sample size formula for a two-sample z-test.
  3. Round the per-group size up to the nearest whole number, then double it. Ceiling-per-group, not ceiling-of-total.
  4. State the formula you use.
  5. Describe how you handle edge cases: delta ≤ 0, sigma == 0, invalid ranges for alpha or power, and insufficient data.

Example

observed = [10, 12, 9, 11, 10.5, 13, 8.5]
alpha    = 0.05
power    = 0.80
delta    = 1.0

What is the minimum total sample size?

Hints

Hint 1

The standard error of the difference between two independent means of equal-sized groups is σ√(2/n). Rearranging for n gives the core formula — make sure the factor of 2 in the numerator is there, or you're solving a one-sample problem.

Hint 2

Two z-quantiles appear in the formula: one for the significance level and one for the power. The power quantile is z_{1−β}, not z_{β}. Using z_{0.2} instead of z_{0.8} gives a negative number and collapses the required sample size.

Hint 3

Ceiling and rounding are not the same. Always apply math.ceil to the per-group n before doubling. If you ceil the total instead you can end up with an odd number that cannot be split into two equal groups that each individually meet the requirement.