Sample Size for a Binomial Margin of Error
Updated
Problem statement
You plan to measure a success rate by running N independent yes-or-no trials. Each trial succeeds with probability p, so the number of successes is binomial. By the central limit theorem, the observed rate is close to normal for large N, and its confidence interval has half-width
h(N) = z * sqrt(p * (1 - p) / N)
where z comes from the confidence level:
- 80%:
z = 1.282 - 90%:
z = 1.645 - 95%:
z = 1.960 - 98%:
z = 2.326 - 99%:
z = 2.576
You are given confidence, the expected rate as pPercent (so p = pPercent / 100), and the target margin as marginPercent (so E = marginPercent / 100). Return the smallest whole number N for which h(N) is strictly less than E. An N where h(N) equals E exactly does not count.
Use the z values exactly as written. The comparison must be exact, so floating-point rounding must not decide the answer.
Function
requiredSampleSize(confidence: int, pPercent: int, marginPercent: int) → intExamples
Example 1
confidence = 95pPercent = 50marginPercent = 2return = 2402Here z = 1.960, p = 0.5 and E = 0.02. The half-width is below E when N > 1.96^2 * 0.25 / 0.02^2 = 2401. At exactly N = 2401 the half-width equals 0.02, which is not strictly less, so the answer is 2402.
Example 2
confidence = 90pPercent = 20marginPercent = 5return = 1741.645^2 * 0.2 * 0.8 / 0.05^2 = 173.1856, so the smallest whole N above it is 174.
Example 3
confidence = 99pPercent = 10marginPercent = 3return = 6642.576^2 * 0.1 * 0.9 / 0.03^2 = 663.5776, so the answer is 664.
Constraints
confidenceis one of 80, 90, 95, 98 and 99.1 <= pPercent <= 991 <= marginPercent <= 50
More Google Onsite Interview questions
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
- Longest Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Nov 2026
- Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Integers With Multiple Sum of Two CubesCharta Health · PHONE SCREEN · ONSITE INTERVIEWCharta Health interview questions
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Integer BreakAmazon · ONSITE INTERVIEW
- Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Path Between Two Nodes of a Fibonacci TreeDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Count Staircases with Strictly Increasing StepseBay · ONSITE INTERVIEWeBay interview questions
- Babysitter Quote with Hourly RatesUpstart · ONSITE INTERVIEWUpstart interview questions
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Maximize the Even-Odd Index Product GapInfosys · ONSITE INTERVIEWInfosys interview questions
- Add Two Numbers IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions