PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Meta

Implement weighted random sampling with preprocessing

Last updated: Mar 29, 2026

Quick Overview

This question evaluates algorithm design and probabilistic reasoning for weighted random sampling, including data-structure preprocessing, time-space trade-offs, expected versus worst-case performance, and attention to numerical precision.

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

Implement weighted random sampling with preprocessing

Company: Meta

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Weighted Random Sampling (with performance follow-up) You are given an array of **positive** weights `w[0..n-1]`. Implement a data structure that supports: - `init(w)`: preprocess the weights. - `pick() -> int`: return an index `i` such that `P(pick() = i) = w[i] / sum(w)`. ### Requirements 1. Provide an approach where `pick()` runs in **O(log n)** time after preprocessing. 2. **Follow-up:** If `pick()` will be called **millions of times**, how would you redesign/precompute so that each `pick()` call is **O(1) expected time** (while keeping preprocessing reasonable)? ### Notes - Assume weights fit in 64-bit integer / double precision. - Clarify how you handle very large `sum(w)` and floating-point pitfalls if using doubles.

Quick Answer: This question evaluates algorithm design and probabilistic reasoning for weighted random sampling, including data-structure preprocessing, time-space trade-offs, expected versus worst-case performance, and attention to numerical precision.

Related Interview Questions

  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve a Key-Door Corridor Maze - Meta (medium)
  • Solve Array Merge and Parentheses Cleanup - Meta (medium)
  • Solve Two Backtracking Array Problems - Meta (hard)
  • Solve Maze and Suffix Problems - Meta (medium)
Meta logo
Meta
Feb 12, 2026, 12:00 AM
Machine Learning Engineer
Onsite
Coding & Algorithms
5
0
Loading...

Weighted Random Sampling (with performance follow-up)

You are given an array of positive weights w[0..n-1]. Implement a data structure that supports:

  • init(w) : preprocess the weights.
  • pick() -> int : return an index i such that P(pick() = i) = w[i] / sum(w) .

Requirements

  1. Provide an approach where pick() runs in O(log n) time after preprocessing.
  2. Follow-up: If pick() will be called millions of times , how would you redesign/precompute so that each pick() call is O(1) expected time (while keeping preprocessing reasonable)?

Notes

  • Assume weights fit in 64-bit integer / double precision.
  • Clarify how you handle very large sum(w) and floating-point pitfalls if using doubles.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Meta•More Machine Learning Engineer•Meta Machine Learning Engineer•Meta 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.