FastPrepUniform Integer from Biased-Coin Flips
Problems/Uniform Integer from Biased-Coin Flips

Uniform Integer from Biased-Coin Flips

Microsoft logoMicrosoft● HardNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Microsoft Data Scientist internship onsite asks how to generate randint(a,b) from Bernoulli(p). Unknown fixed p, independent supplied flips, Von Neumann fair-bit extraction, inclusive endpoints, exact rejection sampling, finite guaranteed input, interface, and bounds are conventional enrichment.

You are given flips, a list of flips from one biased coin. Each flip is true or false. The flips are independent, and the chance of true is the same every time. You do not know that chance, but it is strictly between zero and one (a Bernoulli source).

Return one integer from a to b, both included. Every integer in that range must be equally likely.

First turn the flips into fair bits. Read the flips two at a time. If the two flips are equal, skip them. [false,true] gives the fair bit 0, and [true,false] gives the fair bit 1.

Then use rejection sampling. Take just enough fair bits to write every value from zero to b - a, and read them as one binary number. If the number is in that range, accept it. If not, throw those bits away and try again with the next fair bits. The input always has enough flips to accept a number.

Function

uniformInt(flips: boolean[], a: int, b: int) → int

Examples

Example 1

flips = [false,true,true,false,false,true,true,false]a = 1b = 10return = 6

The flips form the pairs [false,true], [true,false], [false,true], [true,false]. None are equal, so they give the fair bits 0,1,0,1. The range holds ten values, so four bits are needed. Read as a binary number, those bits make 5, which is in range. Adding a gives 6.

Example 2

flips = []a = 5b = 5return = 5

Here a and b are both five, so only one answer is possible. No flips are needed, and the answer is five.

Constraints

  • 1 <= flips.length <= 200000, except a singleton range may use an empty array
  • -10^9 <= a <= b <= 10^9
  • b - a + 1 <= 10^9
  • The supplied flips contain a complete accepted rejection-sampling attempt

More Microsoft Onsite Interview questions

  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Deterministic K-Means AssignmentsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Grouped-Query Attention with a KV CacheAmazon · ONSITE INTERVIEW
  • Maximum Bananas Delivered by a CamelStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
  • Pro-Rata Order BookIMC Trading · ONSITE INTERVIEWIMC Trading interview questions
  • Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
  • Transformer Attention and SwiGLU Forward PassGoogle · ONSITE INTERVIEWGoogle interview questions
  • Vectorized K-Means AssignmentsSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions
  • Fixed-Round K-Means AssignmentsNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
  • Random Bingo Card GeneratorUber · ONSITE INTERVIEWUber interview questions
  • Babysitter Quote with Hourly RatesUpstart · ONSITE INTERVIEWUpstart interview questions

Interview reports that mention this question

  • Microsoft AI/ML Intern Interviews: 114 Experiences (Part 1 of 2)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int uniformInt(boolean[] flips, int a, int b) {
      // Write your code here.
    }

}
Checking account…