Uniform Random Number from Coin Tosses
Updated
Problem statement
You can toss a fair coin as many times as you like, and that is your only source of randomness. Using it, return a random integer between 0 and n, both included, so that each of the n + 1 values is equally likely.
Assumption: so that every answer can be checked, the coin is replaced by the array tosses. Each element is the result of one fair toss, 0 or 1. Use the tosses one at a time, in order, starting with tosses[0], and follow these steps:
- Let
kbe the number of digits ofnwritten in binary. For example,5is101in binary, sok = 3. Forn = 0, takek = 0. - Make an attempt: take the next
ktosses and treat them as a binary number whose highest digit is the first of those tosses. - If that number is at most
n, return it. If it is larger thann, drop it and make a new attempt with thektosses that come after it. A toss is never read twice.
When n = 0, an attempt reads no tosses and gives 0. The array always has enough tosses to reach a number that is at most n; tosses after that one are not used.
Function
uniformRandom(n: int, tosses: int[]) → intExamples
Example 1
n = 5tosses = [1, 1, 0, 0, 0, 1, 1, 1]return = 15 is 101 in binary, so k = 3 and each attempt reads 3 tosses. The first attempt reads 1, 1, 0, which is 110 in binary, or 6. That is larger than 5, so it is dropped. The second attempt reads 0, 0, 1, which is 001, or 1. That is at most 5, so the answer is 1. The last two tosses are not used.
Example 2
n = 10tosses = [1, 1, 0, 0, 1, 0, 1, 0]return = 1010 is 1010 in binary, so k = 4. The first attempt gives 1100, or 12, which is larger than 10 and is dropped. The second gives 1010, or 10. That equals n, which is allowed, so the answer is 10.
Example 3
n = 0tosses = [1]return = 0For n = 0, k = 0, so the first attempt reads no tosses and gives 0. The answer is 0, and the one toss is not used.
Constraints
0 <= n <= 1090 <= tosses.length <= 3000tosses[i]is0or1.- Following the steps above, some attempt gives a number that is at most
nbefore the tosses run out.
More Patreon Phone Screen questions
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
- Wordle Guess CheckerPHONE SCREEN · Seen Jan 2026
- Org Chart Report CountsPHONE SCREEN · Seen Dec 2025
- Format the Missing Numbers from 1 to 99PHONE SCREEN · Seen Sep 2025
- Pythagorean Triplets in an ArrayPHONE SCREEN · Seen Oct 2024
- LRU Cache with TTLPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Password GeneratorPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- Merge IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2022
- JSON ParserPHONE SCREEN · Seen Jun 2021
Similar questions at other companies
- IP Address to CIDR BlocksOpenAI · PHONE SCREENOpenAI interview questions
- Raise a Number to an Integer PowerAdobe · PHONE SCREENAdobe interview questions
- Fewest Games to Find the Top Two TeamsSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
- IP Range to CIDR BlocksAirbnb · PHONE SCREENAirbnb interview questions
- Count the 1 Bits in a 32-Bit ValueAmazon · PHONE SCREEN
- Choose the Next Minesweeper ActionRunloop · PHONE SCREEN
- Power of Two Without DivisionArm · PHONE SCREEN
- Powers of Two in a RangeAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
- Repeated Mirror XORMathWorks · OAMathWorks interview questions