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
November 29, 20260
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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...