FastPrepGenerate an Unbiased Bit from a Biased Coin
Problems/Generate an Unbiased Bit from a Biased Coin

Generate an Unbiased Bit from a Biased Coin

Google logoGoogle● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the von Neumann method but leaves out a function signature and a finite input model. Our version is about 93% the same as the reported question.

An unknown biased coin returns heads with probability p and tails with probability 1 - p, where 0 < p < 1. Consecutive flips are independent.

The array flips is a recorded prefix of coin tosses, where 1 means heads and 0 means tails. Read non-overlapping pairs from left to right:

  • For [1,0], return 0.
  • For [0,1], return 1.
  • For [0,0] or [1,1], discard the pair and continue.

The input is guaranteed to contain a differing pair. Return the unbiased bit produced by the first such pair.

Function

generateUnbiasedBit(flips: int[]) → int

Examples

Example 1

flips = [1,0]return = 0

The first pair is heads-tails, so the method returns 0.

Example 2

flips = [1,1,0,1]return = 1

The equal pair [1,1] is discarded. The next pair is tails-heads, which returns 1.

Example 3

flips = [0,0,1,1,1,0]return = 0

The first two equal pairs are discarded. The final heads-tails pair returns 0.

Constraints

  • 2 <= flips.length <= 200000.
  • flips.length is even.
  • Every value in flips is either 0 or 1.
  • At least one non-overlapping pair contains different values.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • 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

Similar questions at other companies

  • Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
  • Estimate Pi from Uniform SamplesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Add Two Numbers Given as Digit ArraysCapital One · ONSITE INTERVIEWCapital One interview questions
  • Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions
  • Deterministic K-Means AssignmentsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Group-Relative Policy AdvantagesAmazon · ONSITE INTERVIEW
  • Reservoir Sampling with Recorded DrawsStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
  • Sliding-Window Averages as Reduced FractionsCoinbase · ONSITE INTERVIEWCoinbase interview questions
  • Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
  • Vectorized K-Means AssignmentsSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int generateUnbiasedBit(int[] flips) {
        // Write your code here.
    }
}
Checking account…