Uniform Integer from Biased-Coin Flips
Updated
Problem statement
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) → intExamples
Example 1
flips = [false,true,true,false,false,true,true,false]a = 1b = 10return = 6The 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 = 5Here 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^9b - 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