PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/LinkedIn

How do you sample uniformly from an infinite stream?

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of streaming algorithms, randomized sampling and probability, and algorithmic space–time trade-offs involved in maintaining a uniform sample from an unbounded data stream.

  • easy
  • LinkedIn
  • Coding & Algorithms
  • Data Scientist

How do you sample uniformly from an infinite stream?

Company: LinkedIn

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

## Coding (Python) — Random sampling from an infinite stream You receive an **unbounded/infinite stream** of items (e.g., numbers or events). You cannot store all items. ### Task Write Python code to maintain a **uniform random sample** of size `k` from all items seen so far. ### Requirements - Each of the `n` items seen so far should have probability `k/n` of being in the sample. - Use **O(k)** memory. - Stream arrives one item at a time. ### Follow-ups (if time) - Prove/argue why the algorithm is uniform. - What changes if `k=1`? - How would you handle weighted sampling (items have weights)?

Quick Answer: This question evaluates understanding of streaming algorithms, randomized sampling and probability, and algorithmic space–time trade-offs involved in maintaining a uniform sample from an unbounded data stream.

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
Feb 1, 2026, 5:26 PM
Data Scientist
Technical Screen
Coding & Algorithms
10
0
Loading...

Coding (Python) — Random sampling from an infinite stream

You receive an unbounded/infinite stream of items (e.g., numbers or events). You cannot store all items.

Task

Write Python code to maintain a uniform random sample of size k from all items seen so far.

Requirements

  • Each of the n items seen so far should have probability k/n of being in the sample.
  • Use O(k) memory.
  • Stream arrives one item at a time.

Follow-ups (if time)

  • Prove/argue why the algorithm is uniform.
  • What changes if k=1 ?
  • How would you handle weighted sampling (items have weights)?

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.