Reservoir Sampling with Supplied Random Draws
Updated
Problem statement
Given a finite stream, retain exactly k samples using reservoir sampling.
Initialize the reservoir with the first k items. For arrival index i from k onward, draws[i-k] is a supplied uniform draw from 0 through i, inclusive. If the draw is less than k, replace that reservoir slot; otherwise leave the reservoir unchanged. Return reservoir slots in slot order.
Function
reservoirSample(stream: int[], k: int, draws: int[]) → int[]Examples
Example 1
stream = [10,20,30,40,50]k = 3draws = [1,4]return = [10,40,30]The draw for 40 replaces slot 1; the draw for 50 is outside the reservoir and causes no replacement.
Example 2
stream = [7,8]k = 2draws = []return = [7,8]When the stream length equals k, every item remains in the reservoir.
Constraints
1 <= k <= stream.length <= 100000draws.length = stream.length - k- For
0 <= j < draws.length,0 <= draws[j] <= k + j -10^9 <= stream[i] <= 10^9
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Random Bingo Card GeneratorUber · ONSITE INTERVIEWUber interview questions
- Sustained Overspeed AlertsFleetx · ONSITE INTERVIEW
- Generic Stack Operation SequenceArista Networks · PHONE SCREEN · ONSITE INTERVIEWArista Networks interview questions
- Spiral MatrixAthenahealth · ONSITE INTERVIEW
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Tax Calculation with Year and Filing RulesGusto · ONSITE INTERVIEWGusto interview questions
- Propagating Fraud DetectorAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Deep Copy a Random-Pointer ListAmazon · ONSITE INTERVIEW
- Digit-Coded Card ValidationCapital One · ONSITE INTERVIEWCapital One interview questions