Build a Uniform 1-to-10 Picker From a 1-to-7 Random Source

Last updated: November 29, 2026

Quick Overview

You have a function rand7() that returns a uniform integer from 1 to 7. Implement rand10() returning a uniform integer from 1 to 10 using only rand7(), and analyze the expected number of rand7 calls.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


You have a function rand7() that returns a uniform integer from 1 to 7. Implement rand10() returning a uniform integer from 1 to 10 using only rand7(), and analyze the expected number of rand7 calls.

A Meta onsite question that mixes probability with code, relevant to unbiased experiment bucketing and sampling. The interviewer checks that you avoid the biased modulo approach, can prove uniformity, and can compute the expected call count.

What the Interviewer Expects
  • Explain why rand7() + rand7() or modulo tricks are biased
  • Combine two calls into a uniform value on 1..49
  • Use rejection sampling to keep 1..40
  • Compute the expected number of calls
  • Optionally reuse rejected values to cut calls
Key Topics to Cover
Rejection sampling
Probability
Uniform distribution
Expected value
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Reduce the expected calls by recycling the rejected remainder.
  • Build rand7 from rand10.
  • How does this relate to hashing users into experiment buckets?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Given: rand7() uniform on {1..7}, independent across calls. Build rand10() uniform on {1..10}. No other randomness source. Report expected rand7 calls per rand10 call....

High-Level Design

Two calls define v = (r1 - 1) * 7 + r2, which is uniform on 1..49 because each of the 49 (r1, r2) pairs maps to a distinct v. Accept v only if v <= 40, then return (v - 1) % 10 + 1: each output va...


Submit Your Answer
Markdown supported