PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/LinkedIn

Implement stream random sampling in Python

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of streaming algorithms, uniform random sampling, and probabilistic guarantees for maintaining a fixed-size sample from an unbounded data stream (reservoir-sampling concepts).

  • medium
  • LinkedIn
  • Coding & Algorithms
  • Data Scientist

Implement stream random sampling in Python

Company: LinkedIn

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an unbounded stream of items that cannot be stored entirely in memory. Write Python code to maintain a uniform random sample from the stream. The standard form of this problem is reservoir sampling: after seeing `n` items, every item seen so far should have equal probability of being included in a reservoir of size `k`. Your solution should work even when the total stream length is unknown in advance. Explain the algorithm, and discuss its time complexity, space complexity, and any edge cases.

Quick Answer: This question evaluates understanding of streaming algorithms, uniform random sampling, and probabilistic guarantees for maintaining a fixed-size sample from an unbounded data stream (reservoir-sampling concepts).

Related Interview Questions

  • Longest Run of Ones After One Flip - LinkedIn (medium)
  • The Celebrity Problem - LinkedIn (medium)
  • Merge Keyed N-Ary Trees - LinkedIn (medium)
  • Count Trips From Vehicle Logs - LinkedIn (easy)
  • Design O(1) Randomized Multiset - LinkedIn (easy)
|Home/Coding & Algorithms/LinkedIn

Implement stream random sampling in Python

LinkedIn logo
LinkedIn
Oct 12, 2025, 12:00 AM
mediumData ScientistTechnical ScreenCoding & Algorithms
8
0

You are given an unbounded stream of items that cannot be stored entirely in memory. Write Python code to maintain a uniform random sample from the stream.

The standard form of this problem is reservoir sampling: after seeing n items, every item seen so far should have equal probability of being included in a reservoir of size k. Your solution should work even when the total stream length is unknown in advance.

Explain the algorithm, and discuss its time complexity, space complexity, and any edge cases.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

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

Master your tech interviews with 9,000+ 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.