Generate Rand7 from Rand5
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure here is barely one: a flat array of draws you walk two at a time. Bloomberg reported this OA in July 2019, and it's the classic rand7 from rand5 problem dressed up as a stream-consumption function. No tricky state, just rejection sampling. If you've seen it, it's five minutes. If you blank on why the math works, StealthCoder is the safety net running invisibly during the live OA. The real work is knowing the formula and the reject threshold before you start typing.
The problem
draws contains successive results from a uniform rand5() function, each between 1 and 5. Consume draws in pairs to simulate one uniform rand7() result. Return a value from 1 through 7 as soon as rejection sampling accepts a pair. Return -1 if the provided stream ends first. Function rand7FromRand5Draws(draws: int[]) → int Examples Example 1 draws = [5,5,2,3] return = 1 The first pair maps to 24 and is rejected. The second maps to 7, which becomes 1. Constraints 0 <= draws.length <= 10^5. Every draw is between 1 and 5.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is turning two rand5 draws into one uniform number from 1 to 25. Compute (a-1)*5 + b, where a is the first draw and b is the second. Every value from 1 to 25 is equally likely. Accept 1 through 21, since 21 is the largest multiple of 7 that fits, and return ((value-1) % 7) + 1. Reject 22 through 25 and move to the next pair. Check it against the example: [5,5] gives 4*5+5 = 25, which is rejected. [2,3] gives 5+3 = 8, which maps to 1. Wait, the example says the first pair is 24, so the stated mapping may differ, so verify the formula against the sample carefully before trusting your version. Common pitfalls: using the wrong rejection cutoff, which breaks uniformity, and forgetting to return -1 when the stream ends or has a leftover single draw. If you freeze on the mapping, StealthCoder can hand you the formula live.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Generate Rand7 from Rand5 cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as implement rand10 using rand7. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Generate Rand7 from Rand5 FAQ
What's the trick to rand7 from rand5?+
Combine two rand5 draws into one uniform value from 1 to 25, then reject anything above 21. Take the remainder mod 7 and add 1 for the result. Rejecting the top four values keeps every outcome equally likely. That's the whole idea.
Why can't I just add two rand5 results?+
Adding two draws gives a bell-shaped distribution, not a uniform one. A sum of 6 is far more likely than a sum of 2. You need a mapping where each pair produces a distinct value, like (a-1)*5 + b.
What should the function return if draws run out?+
Return -1. That covers an empty array, a stream where every pair was rejected, and a leftover single draw with no partner. Loop while at least two draws remain, and fall through to -1 after the loop.
How hard is this one really?+
Easy once you know the formula, awkward if you've never seen rejection sampling. The code is a short loop with a stride of two. The risk is the math, not the implementation. Test it against the provided example before submitting.
How do I prepare for this in 48 hours?+
Write the function from memory twice. Make sure you can explain why 21 is the cutoff and why sums fail. Then test edge cases: empty input, one draw, all rejected pairs, and a first-pair accept. That covers what this OA is likely checking.