Implement Rand10() Using Rand7()
A medium-tier problem at 46% community acceptance, tagged with Math, Rejection Sampling, Randomized. Reported in interviews at Tencent and 2 others.
You've got a function that returns a random integer 1-7. Now you need to build one that returns 1-10 using only that. Sounds simple until you realize you can't just call it once or twice. Tencent, DE Shaw, and Yandex have all asked this. The trap is thinking there's a clean mathematical way to map 7-range to 10-range. There isn't. The real solution uses rejection sampling, a probabilistic technique that most candidates don't see coming. If this hits your live assessment and you're stuck on the math, StealthCoder solves it invisibly in seconds.
Companies that ask "Implement Rand10() Using Rand7()"
Implement Rand10() Using Rand7() is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.
Get StealthCoderThe core trick is that calling Rand7() twice gives you values 1-49 with uniform distribution. You can map those 49 outcomes to 1-10 by accepting outcomes 1-40 (which divide evenly by 10 and give 10 equally-likely groups) and rejecting outcomes 41-49, then recursively retrying. That's rejection sampling. The naive mistake is trying to use modulo or division directly on the 7-range without understanding why that breaks uniformity. Rejection Sampling as a topic captures the unintuitive part: you're throwing away some calls to Rand7() to guarantee the final distribution stays flat. Most engineers freeze on this in real time because it feels inefficient. StealthCoder hedges that knowledge gap by showing the pattern instantly.
Pattern tags
You know the problem.
Make sure you actually pass it.
Implement Rand10() Using Rand7() recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Implement Rand10() Using Rand7() interview FAQ
Why can't I just call Rand7() and use math to map it to 1-10?+
Because 7 doesn't divide evenly into 10. Any direct mapping (like (Rand7() % 10) + 1) creates bias toward certain numbers. Rejection Sampling fixes this by using multiple calls to create a larger uniform space (1-49) that does divide evenly into 10, then rejecting the rest.
Is this still asked at top tech companies?+
Yes. Tencent, DE Shaw, and Yandex have all reported this problem. It's lower-frequency than graph or dynamic programming problems, but it's a legit curveball in online assessments. Acceptance rate is below 46%, so most candidates miss it.
What's the Probability and Statistics angle here?+
Understanding why your output distribution must stay uniform is pure probability. Each of the 10 outputs must have exactly 1-in-10 odds. Rejection Sampling enforces that by discarding non-uniform outcomes. The math proves that retrying on rejection preserves uniformity.
Does my solution need to handle edge cases?+
Not really. The function just needs to return 1-10 with equal probability and terminate. Rejection Sampling always terminates eventually because each call has a >80% acceptance rate. No boundary checks needed.
How do I avoid timeout on this problem?+
You won't timeout if you use rejection sampling correctly. Expected calls to Rand7() per output is around 1.2. That's fast. The trap is trying to over-engineer it or recursing without understanding why the algorithm terminates reliably.
Want the actual problem statement? View "Implement Rand10() Using Rand7()" on LeetCode →