Uniform 0-6 from an Unknown Biased Bit
Reported by candidates from LinkedIn's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
LinkedIn reported this one in September 2026, and the title sounds scarier than the code. The setup is a von Neumann extractor feeding a rejection sampler, and the real hinge is bit manipulation: you build integers out of bits, most-significant first. If you've got an OA invite and 48 hours, know this shape cold. Two tapes, two simulations, one returned pair. StealthCoder sits invisibly as a safety net if you blank mid-assessment, but the logic below is short enough to hold in your head.
The problem
You receive two deterministic tapes that represent successive calls to random-bit generators. biasedBits comes from one unknown but fixed source bias q, where 0 < q < 1: 0 has probability q, and 1 has probability 1-q. uniformBits comes from a fair bit generator. First, consume biasedBits in consecutive pairs. Discard equal pairs; map [0,1] to fair bit 0 and [1,0] to fair bit 1. Use fair bits most-significant first in groups of three, rejecting 7, and produce a uniform value in [0,6]. For the follow-up, let the target ratio p = pNumerator / pDenominator. Consume the minimum-width groups from uniformBits, reject integers outside [0,pDenominator-1], and return 0 when the accepted integer is less than pNumerator, otherwise 1. Return [uniformValue, biasedValue]. Each tape is guaranteed to contain enough bits. Function generateRandomOutputs(biasedBits: int[], uniformBits: int[], pNumerator: int, pDenominator: int) → int[] Examples Example 1 biasedBits = [0,1,1,0,0,1] uniformBits = [0,1] pNumerator = 1 pDenominator = 4 return = [2,1] The biased pairs yield fair bits 0,1,0, which form 2. Fair bits 0,1 form integer 1. For p=1/4, only integer 0 maps to 0, so the follow-up output is 1. Example 2 biasedBits = [1,0,1,0,1,0,0,1,1,0,0,1] uniformBits = [1,1,1,0] pNumerator = 2 pDenominator = 3 return = [2,1] The first three extracted bits form 7 and are rejected; the next three form 2. The fair-bit candidate 3 is rejected for denominator 3, then candidate 2 maps to 1. Constraints biasedBits.length and uniformBits.length are between 0 and 100000. Every tape element is 0 or 1, and each tape is long enough for its required accepted sample. 1 <= pDenominator <= 100 0 <= pNumerator <= pDenominator
Reported by candidates. Source: FastPrep
Pattern and pitfall
Part one is the von Neumann trick. Walk biasedBits two at a time. Equal pairs get discarded, [0,1] gives fair bit 0, [1,0] gives fair bit 1. Collect fair bits until you have three, shift them into a value (v = v*2 + bit), and reject 7 by restarting the group. Part two is rejection sampling on uniformBits. Compute the minimum bit width k so that 2^k >= pDenominator, read k bits MSB first, and reject anything >= pDenominator. Then return 0 if the value is below pNumerator, else 1. The pitfall is the width: for denominator 1, k is 0, so you read nothing and the value is 0. Another trap is resetting the group after a rejection, and keeping separate pointers for each tape. Both are O(n) single passes. StealthCoder is the hedge if the pointer bookkeeping slips live.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Uniform 0-6 from an Unknown Biased Bit 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass LinkedIn's OA.
LinkedIn reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Uniform 0-6 from an Unknown Biased Bit FAQ
What's the trick in this LinkedIn OA question?+
Two separate simulations. The biased tape uses the von Neumann extractor: pairs 01 and 10 become fair bits, equal pairs are dropped. The uniform tape uses rejection sampling with the smallest bit width that covers the denominator. Build integers with shifts, MSB first.
How do I pick the bit width for the follow-up?+
Find the smallest k where 2^k is at least pDenominator. Read k bits from uniformBits, MSB first, and form an integer. If it's pDenominator or larger, throw it away and read another group. For a denominator of 1, k is 0 and the value is 0.
Why does Example 2 reject the first three fair bits?+
The first three extracted fair bits are 1,1,1, which equals 7. The three-bit sampler must reject 7 so the remaining values 0 through 6 stay uniform. You discard the whole group and start a fresh group of three from the next fair bits.
Is the bias q something I need to compute?+
No. q is unknown and fixed, and the extractor works for any q because 01 and 10 are equally likely. You never estimate it. Just read pairs, drop equal ones, and map the unequal ones to 0 or 1.
How should I prepare in 48 hours?+
Write this once from scratch with two independent pointers and test both examples by hand. Then check edge cases: empty uniformBits with denominator 1, numerator 0, numerator equal to denominator, and several rejections in a row. It's a one-pass simulation, so it's quick to rehearse.