Reported January 2026
Googlesimulation

Reservoir Sampling with Recorded Draws

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this Google OA, reported January 2026, is an off-by-k error on the draws array. Candidates index draws[i] instead of draws[i - k] and get garbage. It's a plain simulation problem dressed up as reservoir sampling. No randomness, no probability math, just replay the recorded draws against the reservoir. If you read it carefully, it's a ten-line solution. If you rush, you burn twenty minutes debugging an index. StealthCoder sits invisibly on your screen as a safety net if you blank during the live OA, but this one is very doable on your own.

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 trick is that there's no trick. 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]. Return the reservoir. That's O(n) time and O(k) space. The pitfall is the index offset. draws has length n - k, so draws[i] will run out of bounds or read the wrong draw. Second pitfall: the replace slot is the draw value itself, not i mod k and not a fresh random number. Don't call a random function at all. Third, watch the k equals n case, where draws is empty and the loop never runs. Trace Example 3 by hand before submitting. If the live OA has you second-guessing the offset, StealthCoder is the hedge that reads the problem and hands you the clean loop.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

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 StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google 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

How hard is the Google reservoir sampling with recorded draws problem really?+

Easy on logic, easy to botch on indexing. There's no real sampling, since the draws are given. You replay them. The only difficulty is mapping array index i to draws[i - k] and reading the replace-or-skip rule correctly. Most failures come from rushing the setup, not the algorithm.

What's the trick to solving it?+

Treat it as a direct simulation. Initialize the reservoir with values[0..k-1], then for each i from k onward, take d = draws[i - k]. If d is less than k, overwrite reservoir[d] with values[i]. Otherwise skip. That's the whole solution in one pass.

What edge cases should I test?+

Test k equal to values.length, where draws is empty and you return the first k values unchanged. Test k = 1, where every replacement hits slot 0. Test a draw exactly equal to k, which must be skipped. Also confirm negative values and large magnitudes work, since values go up to a billion.

What's the time and space complexity?+

Time is O(n) since you touch each element once after the first k. Space is O(k) for the reservoir, or O(n) if you naively copy the whole array. With n up to 200000 either fits, but a single pass with a k-sized array is the clean answer.

How do I prepare for this in 48 hours?+

Don't study reservoir sampling theory. Write this simulation from scratch twice, then hand-trace the three examples, especially Example 1 with the skipped draw. Practice other index-offset simulation problems for an hour. The goal is clean loop bounds and no off-by-k slips under pressure.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it