Experiment Sample Size and Runtime Calculator
Arrays & HashingAuto-graded · 19 test casesLast 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
| Name | Type | Description |
|---|---|---|
baseline_value | float | The current average value of the metric being measured |
mde_percent | float | Minimum detectable effect as a fraction of the baseline (e.g. 0.02 means 2 %) |
stddev | float | Standard deviation of the metric |
dau | int | Daily active users across the entire product |
exposure_rate | float | Fraction 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.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem breaks into two independent calculations: how many observations each variant needs, and how many days it takes to collect them.
Step 1 — Per-variant sample size
The standard two-sample test for comparing means under a normal approximation gives:
$$n = \left\lceil \frac{2,\sigma^2,(Z_{\alpha/2} + Z_{\beta})^2}{\delta^2} \right\rceil$$
Convert the percentage MDE to absolute units first (delta = baseline_value * mde_percent), because stddev is in absolute units and the formula requires consistency. With the fixed constants $Z_{\alpha/2} = 1.96$ and $Z_{\beta} = 0.84$, their sum is 2.80 and its square is 7.84. Plug those in and ceiling the result.
Why ceiling? You cannot observe a fractional user. Under-collecting by even one observation technically leaves you short of the declared power, so rounding up is the correct conservative choice.
Step 2 — Minimum duration
Each day, dau × exposure_rate eligible users enter the experiment. They are split evenly, so each variant receives half that count per day. Dividing sample_per_variant by the per-variant daily inflow and ceiling gives the minimum number of whole days.
This is a pure arithmetic result. In practice you would run longer to capture weekly seasonality and let novelty effects stabilize, but the function is only asked for the mathematical minimum.
Why this is correct
The formula is the standard result from the Neyman–Pearson framework for two independent samples with equal allocation. Fixing $\alpha = 0.05$ (two-sided) and power $= 0.80$ is encoded directly in the z-scores. The ceil operations ensure both guarantees — power and whole-day granularity — are never violated by rounding down.
Complexity
- Time: O(1) — a fixed number of arithmetic operations regardless of input size.
- Space: O(1) — only scalar intermediates and the two-key output dictionary.
Reference solution
import math
from typing import Dict
def experiment_size_and_duration(
baseline_value: float,
mde_percent: float,
stddev: float,
dau: int,
exposure_rate: float,
) -> Dict[str, int]:
# Fixed z-scores: two-sided alpha = 0.05, power = 0.80
z_alpha_half = 1.96
z_beta = 0.84
# Absolute minimum detectable effect
delta = baseline_value * mde_percent
# Two-sample normal approximation, equal allocation, ceil to whole users
sample_per_variant = math.ceil(
2 * (stddev ** 2) * ((z_alpha_half + z_beta) ** 2) / (delta ** 2)
)
# Eligible users split evenly across the two variants
daily_eligible = dau * exposure_rate
per_variant_per_day = daily_eligible / 2
# Minimum whole days required
days = math.ceil(sample_per_variant / per_variant_per_day)
return {
"sample_per_variant": sample_per_variant,
"days": days,
}