PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms

Prove reservoir sampling correctness

Last updated: Jul 4, 2026

Quick Overview

This question evaluates randomized and streaming algorithm skills, including mastery of reservoir sampling, induction-based correctness proofs, probability reasoning, and complexity analysis for sampling under strict memory constraints.

  • medium
  • Coding & Algorithms
  • Data Scientist

Prove reservoir sampling correctness

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Design an algorithm to sample k items uniformly at random from a stream of unknown and potentially massive length N, using O(k) memory and one pass. (a) Write the algorithm for k=1 and generalize to k>1. (b) Prove by induction that after processing i items, each seen item has probability k/i to be in the reservoir. (c) Discuss time and memory complexity, and outline how you would adapt it for weighted sampling.

Quick Answer: This question evaluates randomized and streaming algorithm skills, including mastery of reservoir sampling, induction-based correctness proofs, probability reasoning, and complexity analysis for sampling under strict memory constraints.

Related Interview Questions

  • Merge Two Sorted Arrays - Whatnot (hard)
  • Remove All Adjacent Duplicate Pairs - Whatnot (hard)
  • Implement Adjacent-Line Uniq - Vanta (medium)
  • Perform an External Merge Sort with a Heap - Citadel (medium)
  • Simulate a TTL Cache with LRU Eviction - Netflix (medium)
|Home/Coding & Algorithms

Prove reservoir sampling correctness

Oct 13, 2025, 9:49 PM
mediumData ScientistOnsiteCoding & Algorithms
3
0

Design an algorithm to sample k items uniformly at random from a stream of unknown and potentially massive length N, using O(k) memory and one pass. (a) Write the algorithm for k=1 and generalize to k>1. (b) Prove by induction that after processing i items, each seen item has probability k/i to be in the reservoir. (c) Discuss time and memory complexity, and outline how you would adapt it for weighted sampling.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Data Scientist•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.