PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/LinkedIn

Sample index from probability distribution

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in randomized algorithms and probability-based sampling, along with algorithmic preprocessing and data-structure design for efficient repeated sampling.

  • medium
  • LinkedIn
  • Coding & Algorithms
  • Machine Learning Engineer

Sample index from probability distribution

Company: LinkedIn

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given a discrete probability distribution for an \(M\)-sided die as an array `p[0..M-1]`, where each `p[i]` is non-negative. Design a function `sample(p) -> int` that returns a random index `i` such that: - \(\Pr(sample(p)=i) = p[i] / \sum_j p[j]\) ### Requirements - The function will be called many times with the same `p`. - Aim for \(O(\log M)\) time per sample after preprocessing. ### Follow-up What if the probabilities do **not** sum to 1? Explain how your approach handles this. ### Clarifications - Assume you have access to a random number generator that can produce a uniform float in \([0,1)\) or a uniform integer in a range. - If \(\sum_j p[j] = 0\), you may define behavior (e.g., throw an error).

Quick Answer: This question evaluates proficiency in randomized algorithms and probability-based sampling, along with algorithmic preprocessing and data-structure design for efficient repeated sampling.

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 11, 2026, 12:00 AM
Machine Learning Engineer
Onsite
Coding & Algorithms
29
0
Loading...

You are given a discrete probability distribution for an MMM-sided die as an array p[0..M-1], where each p[i] is non-negative.

Design a function sample(p) -> int that returns a random index i such that:

  • Pr⁡(sample(p)=i)=p[i]/∑jp[j]\Pr(sample(p)=i) = p[i] / \sum_j p[j]Pr(sample(p)=i)=p[i]/∑j​p[j]

Requirements

  • The function will be called many times with the same p .
  • Aim for O(log⁡M)O(\log M)O(logM) time per sample after preprocessing.

Follow-up

What if the probabilities do not sum to 1? Explain how your approach handles this.

Clarifications

  • Assume you have access to a random number generator that can produce a uniform float in [0,1)[0,1)[0,1) or a uniform integer in a range.
  • If ∑jp[j]=0\sum_j p[j] = 0∑j​p[j]=0 , you may define behavior (e.g., throw an error).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More LinkedIn•More Machine Learning Engineer•LinkedIn Machine Learning Engineer•LinkedIn Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

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