Reported September 2026
StackAdaptsimulation

Reservoir Sampling with Recorded Draws

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

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

The StackAdapt OA reported in September 2026 dresses up a simple simulation in reservoir sampling vocabulary. Don't let the name scare you. There's no randomness to implement, because the draws are handed to you in an array. You fill the reservoir with the first k values, then walk the rest of the stream and check each recorded draw. If you're taking this in the next day or two, expect a short, clean loop with a few traps around index math. StealthCoder sits invisibly on your screen as a safety net if you blank on the offsets during the live OA.

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

What it really reduces to: copy the first k values, then loop i from k to n-1. For each i, read draws[i - k]. If it's less than k, set reservoir[draw] = values[i]. That's it. The draw value itself is the slot index, and that's the detail people miss. They assume the slot is something else or that they need to generate a random number. The main pitfall is the offset. draws has length n - k, so draws[i - k] lines up with index i. Off-by-one there breaks Example 1. Runtime is O(n) time and O(k) space, which comfortably handles 200000 elements. Skip any sorting or hashing. Trace Example 3 by hand before submitting. If the indexing blurs under pressure, StealthCoder is the hedge during the live OA, reading the problem and giving you the loop.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

StackAdapt 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.

Reservoir Sampling with Recorded Draws FAQ

How hard is the StackAdapt reservoir sampling question really?+

Easy once you see it's a plain simulation. The draws are given, so there's no random number generation and no probability math. It's one copy and one loop. The difficulty is only in reading the statement carefully and getting the index offset right.

What's the trick to this problem?+

The recorded draw is the slot to overwrite, but only when it's smaller than k. Otherwise you skip the element. Map draws[i - k] to values[i] and you're done. No need to compute any probabilities.

What's the time and space complexity?+

Time is O(n) because you touch each value once after the initial copy. Space is O(k) for the reservoir, or O(n) if you copy the whole array carelessly. Both are fine for n up to 200000, so nothing fancy is required.

What edge cases should I test?+

Test k equal to the array length, where draws is empty and you return the first k values unchanged. Test k = 1, as in Example 3. Also test a draw exactly equal to k, which must be skipped, since the condition is strictly smaller than k.

How do I prepare for this in 48 hours?+

Write the loop once from memory and trace all three examples by hand. Focus on the draws[i - k] offset and the strict less-than check. Then practice a few other simulation-style array problems so you stay calm reading wordy statements that hide simple logic.

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

OA at StackAdapt?
Invisible during screen share
Get it