KatInterview

Experiment Sample Size and Runtime Calculator

Arrays & HashingAuto-graded · 19 test cases
HardAuto-gradedArrays & Hashingstatisticsexperimentationa-b-testing

Last updated

Tests ability to apply the two-sample normal approximation for A/B test sizing and convert a required sample count into a minimum experiment duration.

Background

Your team is evaluating a change to a product feature and wants to run a controlled experiment to detect whether the new version shifts the average transaction value. Before launching, you need to determine how large each group must be and how many days the experiment must run to collect enough data.

Use the two-sided, two-sample normal approximation with equal allocation between control and treatment:

$$n = \left\lceil \frac{2,\sigma^2,(Z_{\alpha/2} + Z_{\beta})^2}{\delta^2} \right\rceil$$

where:

  • $\sigma$ is the standard deviation of the metric
  • $\delta$ is the minimum detectable effect (MDE) expressed in absolute units: $\delta = \text{baseline} \times \text{mde_percent}$
  • $Z_{\alpha/2} = 1.96$ (two-sided, $\alpha = 0.05$)
  • $Z_{\beta} = 0.84$ ($\text{power} = 0.80$)

The number of eligible users entering the experiment each day is:

$$\text{daily eligible} = \text{dau} \times \text{exposure_rate}$$

These users are split evenly between the two variants, so each variant receives half that daily flow. Divide the per-variant sample requirement by the per-variant daily inflow and round up to get the minimum number of days needed.

Task

Write a function that accepts the parameters below and returns a dictionary with exactly two integer keys:

  • "sample_per_variant" — the required sample size per variant (rounded up to the nearest integer)
  • "days" — the minimum whole number of days needed to reach that sample size

Parameters

NameTypeDescription
baseline_valuefloatThe current average value of the metric being measured
mde_percentfloatMinimum detectable effect as a fraction of the baseline (e.g. 0.02 means 2 %)
stddevfloatStandard deviation of the metric
dauintDaily active users across the entire product
exposure_ratefloatFraction of DAU who are eligible to enter the experiment

Return value

{"sample_per_variant": int, "days": int}

Both values are positive integers. Use math.ceil for all rounding.

Constraints

  • $10 \le \text{baseline_value} \le 10{,}000$
  • $0 < \text{mde_percent} \le 0.50$
  • $1 \le \text{stddev} \le 10{,}000$
  • $1{,}000 \le \text{dau} \le 10{,}000{,}000$
  • $0 < \text{exposure_rate} \le 1.0$
  • $Z_{\alpha/2}$ and $Z_{\beta}$ are fixed constants: 1.96 and 0.84 respectively
  • Always use two-sided testing and equal allocation

Examples

Example 1

Input: experiment_size_and_duration(100, 0.02, 40, 500000, 0.2)

Output: {'sample_per_variant': 6272, 'days': 1}

Example 2

Input: experiment_size_and_duration(100, 0.02, 40, 1000000, 0.1)

Output: {'sample_per_variant': 6272, 'days': 1}

Example 3

Input: experiment_size_and_duration(50, 0.05, 15, 200000, 0.5)

Output: {'sample_per_variant': 565, 'days': 1}

Hints

Hint 1

Compute the absolute MDE first: multiply baseline_value by mde_percent. The formula works in absolute units, not percentages.

Hint 2

The combined z-score term appears squared in the numerator. Calculate (1.96 + 0.84)² once and reuse it.

Hint 3

Daily inflow per variant is half the total eligible users per day. Divide sample_per_variant by that half-daily figure, then ceil the result.