BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Probability and statistics17 of 24 in topic

68. Compute the sample size per group for a two-proportion A/B test

MediumProbability and statistics
Asked atMicrosoft

Why it matters Every product team running conversion experiments needs this number to know how long a test must run before results mean anything.

Before launching an A/B test you need to know how many users each arm needs to detect the lift you care about.

Write ab_sample_size(p1, p2, alpha=0.05, power=0.8). p1 is the baseline conversion rate and p2 the rate you want to detect. Use a two-sided test: z_a = Phi^-1(1 - alpha / 2), z_b = Phi^-1(power), with Phi^-1 from statistics.NormalDist().inv_cdf. With pbar = (p1 + p2) / 2, n = (z_a * sqrt(2 pbar (1 - pbar)) + z_b * sqrt(p1 (1 - p1) + p2 (1 - p2)))^2 / (p1 - p2)^2. Return ceil(n) as an int. Raise ValueError if p1 == p2.

Functiondef ab_sample_size(p1, p2, alpha=0.05, power=0.8)

Example 1

Input: p1 0.10, p2 0.12

Output: 3841

Example 2

Input: p1 = 0.2, p2 = 0.3

Output: 294

Example 3

Input: p1 = 0.05, p2 = 0.06, alpha = 0.01, power = 0.9

Output: 15465

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

Two z-values, two standard deviation terms, one square, then round up.

Hint 2

from statistics import NormalDist.

Hint 3

Two-sided: 1 - alpha / 2.

Hint 4

Use math.ceil at the end.

Common mistakes
  • Using a one-sided z for a two-sided test.
  • Forgetting to square the lift in the denominator.
  • Stopping the test early as soon as it looks significant.
Understand the idea

Small lifts need many users. To detect conversion moving from 10% to 12%, random noise must be small compared with a 2 point gap, and noise only shrinks like 1 / sqrt(n).

Two settings control the answer. alpha is the false alarm rate (5% means you accept a 1 in 20 chance of calling a fake win). Power is the chance of catching a real lift of that size (80% is common). Each becomes a z-value: about 1.96 for a two-sided 5% alpha and about 0.84 for 80% power.

Worked example

p1 = 0.10, p2 = 0.12, pbar = 0.11. First term 1.96 * sqrt(2 * 0.11 * 0.89) = 1.96 * 0.4425 = 0.867. Second term 0.84 * sqrt(0.09 + 0.1056) = 0.84 * 0.4423 = 0.372. Sum 1.239, squared 1.536, divided by 0.0004 gives about 3,841 users per arm.

n = (z_a sqrt(2 pbar(1 - pbar)) + z_b sqrt(p1(1 - p1) + p2(1 - p2)))^2 / (p1 - p2)^2

Halving the lift roughly quadruples the sample size.

Topics
Probability and statistics
Companies1
  • Microsoft the topic appears in interview reports Microsoft Interview Prep: Questions, Process & Study Plan

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Decide if a new prompt really converts better with a two-proportion z-testMedium
  • Update your belief about a model's pass rate with a Beta priorMedium
  • Put a bootstrap confidence interval on an evaluation scoreMedium
  • Compare two eval runs with Welch's t-test statisticMedium
All Probability and statistics problems
Report
68 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
7
import math
from statistics import NormalDist
​
def ab_sample_size(p1, p2, alpha=0.05, power=0.8):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS