FastPrepUniform Random Number from Coin Tosses
Problems/Uniform Random Number from Coin Tosses

Uniform Random Number from Coin Tosses

Patreon logoPatreon● MediumFULLTIMEPHONE SCREEN

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: One report gives this task: a Senior Machine Learning Engineer candidate who interviewed in February 2026 and did not get an offer. Their process was a recruiter call, a tech screen, a hiring manager talk and a three-hour onsite. Before the screen, their recruiter had said the tech screen would be half coding in Python and half machine learning system design; the candidate writes that the coding turned out to be an algorithm task built on bit shifts, plus probability and math they had not prepared for. The one question the report lists: with a coin-toss function as the only source of randomness, each toss being fair, write code that gives back a uniformly random number between 0 and N. We place it in the tech screen (phone screen stage), because the report's account of that screen fits this question. Our choices: the answer is a whole number, and both 0 and N are possible answers; instead of calling a real coin, the function gets the toss results as a list of 0s and 1s and uses them in order, so every answer can be checked; each attempt reads as many tosses as n has binary digits (none when n is 0), the first toss giving the highest digit, and an attempt whose number is above n is thrown away and a new one starts with the tosses that follow; the list always holds enough tosses, and any left over are ignored. The limits on n and on the list length, and the examples, are ours.

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:

  1. Let k be the number of digits of n written in binary. For example, 5 is 101 in binary, so k = 3. For n = 0, take k = 0.
  2. Make an attempt: take the next k tosses and treat them as a binary number whose highest digit is the first of those tosses.
  3. If that number is at most n, return it. If it is larger than n, drop it and make a new attempt with the k tosses 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[]) → int

Examples

Example 1

n = 5tosses = [1, 1, 0, 0, 0, 1, 1, 1]return = 1

5 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 = 10

10 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 = 0

For 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 <= 109
  • 0 <= tosses.length <= 3000
  • tosses[i] is 0 or 1.
  • Following the steps above, some attempt gives a number that is at most n before 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
All Patreon interview questionsSee Patreon hiring insights
Saved
class Solution {
    public int uniformRandom(int n, int[] tosses) {
        // Write your code here.
    }
}
Checking account…