Reservoir Sampling with Recorded Draws
Reported by candidates from Meta's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Meta's September 2026 OA reports include a reservoir sampling problem where the random draws are handed to you as an array, so there's no randomness left. It's pure simulation. The catch is the indexing. The draw for index i lives at draws[i - k], and the replacement slot is the draw value itself, only when it's below k. Miss that offset and your output is quietly wrong on the examples. If you blank mid-assessment, StealthCoder is the safety net running invisibly on your desktop. But this one is short enough to own before you sit down.
The problem
Simulate size-k reservoir sampling over the integer array values. Begin with the first k values in reservoir slots 0 through k - 1. For every later array index i, draws[i - k] records an integer drawn from the inclusive range [0, i]. If that recorded draw is smaller than k, replace that reservoir slot with values[i]. Otherwise leave the reservoir unchanged. Return the final reservoir in slot order. Function reservoirSample(values: int[], k: int, draws: int[]) → int[] Examples Example 1 values = [10,20,30,40,50] k = 2 draws = [0,2,1] return = [30,50] Index 2 replaces slot 0 with 30. The draw for index 3 is not smaller than 2, so 40 is skipped. Index 4 then replaces slot 1 with 50. Example 2 values = [1,2,3] k = 3 draws = [] return = [1,2,3] The reservoir already contains the complete stream, so no recorded draws are needed. Example 3 values = [5,6,7,8] k = 1 draws = [1,0,3] return = [7] Value 6 is skipped, value 7 replaces slot 0, and value 8 is skipped. Constraints 1 <= values.length <= 200000 1 <= k <= values.length draws.length = values.length - k For every j, 0 <= draws[j] <= k + j. -1000000000 <= values[i] <= 1000000000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is simulation over an array. Copy the first k values into the reservoir. Then loop i from k to n-1, read d = draws[i - k], and if d < k, set reservoir[d] = values[i]. Otherwise do nothing. That's O(n) time and O(k) space, which easily handles 200000 elements. The pitfall is the offset. People index draws[i] and run off the end, or they treat the draw as a probability check instead of the slot index. Also note k equal to n means draws is empty, so the loop never runs and you return the first k values. Check Example 3 by hand: k=1, draws [1,0,3], only the second draw is below 1, so 7 wins. If the offset trips you up under the clock, StealthCoder can hand you the clean loop as a hedge during the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Reservoir Sampling with Recorded Draws 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Meta's OA.
Meta reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Reservoir Sampling with Recorded Draws FAQ
What's the trick in the Meta reservoir sampling OA?+
There isn't a hard trick. The draws are given, so it's a straight simulation. Fill the reservoir with the first k values, then for each later index use draws[i - k] as both the test and the slot. If it's less than k, overwrite that slot with values[i].
What's the most common bug on this problem?+
Off-by-k indexing. The draws array has length n - k, so draws[i] is wrong for i starting at k. Use draws[i - k], or loop j over draws and read values[j + k]. Test it against Example 1 to confirm the offset.
How hard is this really?+
Easy once you read carefully. The logic is a single loop with one comparison. The difficulty is the wording, since the statement mixes reservoir sampling vocabulary with recorded draws. Strip the theory and it's array replacement.
What edge cases should I test?+
Test k equal to values.length, where draws is empty and you return the values unchanged. Test k = 1, where only a draw of 0 replaces anything. Also test duplicate replacements to the same slot, where the later value must win.
How do I prepare in 48 hours?+
Write the loop from memory twice, then trace the three given examples by hand. Add a test where every draw is below k and one where none are. Use fast IO if your language needs it, though O(n) with 200000 elements is fine.