FastPrepReservoir Sampling with Supplied Random Draws
Problems/Reservoir Sampling with Supplied Random Draws

Reservoir Sampling with Supplied Random Draws

Microsoft logoMicrosoft● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Microsoft Bing Applied Scientist internship onsite asks candidates to keep K items sampled with equal chance from a stream. It gives the K/N rule for taking an item. We chose a finite stream, uniform integer draws given as input, the limits on k, the slot order and the function signature.

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 <= 100000
  • draws.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

Interview reports that mention this question

  • Microsoft AI/ML Intern Interviews: 114 Experiences (Part 1 of 2)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int[] reservoirSample(int[] stream, int k, int[] draws) {
      // Write your code here.
    }

}
Checking account…