PracHub
QuestionsPremiumCoachesLearningGuidesInterview 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

  • Count Trips From Vehicle Logs - LinkedIn (easy)
  • Design O(1) Randomized Multiset - LinkedIn (easy)
  • Process Mutable Matrix Sum Queries - LinkedIn (medium)
  • Design a Randomized Multiset - LinkedIn (medium)
  • Can You Place N Objects? - LinkedIn (medium)
LinkedIn logo
LinkedIn
Oct 12, 2025, 12:00 AM
Data Scientist
Technical Screen
Coding & Algorithms
6
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 8,000+ real questions from top companies.

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL 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.