FastPrepSample Size for a Binomial Margin of Error
Problems/Sample Size for a Binomial Margin of Error

Sample Size for a Binomial Margin of Error

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This was a statistics question in a role-related knowledge round for a Google FDE (GenAI) role. The goal was a sample size so that the confidence-interval half-width is under 2%. It was worked out with a binomial model and the central limit theorem.

The report does not give the confidence level, the expected rate, or a function signature. Here, the half-width is the normal approximation z * sqrt(p(1 - p) / N). The value z comes from five confidence levels, rounded to three decimals, which can change answers. The rate and the margin are whole percents. The answer is the smallest N whose half-width is strictly below the margin, so an exact tie does not count. Example 1 pairs the 2% margin with the common defaults of 95% confidence and p = 0.5. The limits are our choice.

Our version is about 75% the same as the reported question.

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) → int

Examples

Example 1

confidence = 95pPercent = 50marginPercent = 2return = 2402

Here 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 = 174

1.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 = 664

2.576^2 * 0.1 * 0.9 / 0.03^2 = 663.5776, so the answer is 664.

Constraints

  • confidence is one of 80, 90, 95, 98 and 99.
  • 1 <= pPercent <= 99
  • 1 <= 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

Interview reports that mention this question

  • Google FDE and Cloud AI, Experienced: 6 Experiences (Part 4 of 4)

Google interview experiences

  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
  • Google AI Coding Interview and AI OA: 2 Experiences
  • Google SWE Intern Interview Results Timeline: 13 Experiences
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int requiredSampleSize(int confidence, int pPercent, int marginPercent) {
        // Write your code here.
    }
}
Checking account…