PracHub
QuestionsCoachesLearningGuidesInterview Prep

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.

Preprocess positive weights with prefix sums. For each supplied draw value in [0,total), return the index selected by weighted sampling. This deterministic form makes the random picker exact-match testable.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ([1,3,2], [0,1,3,4,5])

Expected Output: [0, 1, 1, 2, 2]

Explanation: Draws map into prefix ranges.

Input: ([5], [0,4])

Expected Output: [0, 0]

Explanation: Single bucket.

Input: ([2,2,2], [0,2,4])

Expected Output: [0, 1, 2]

Explanation: Equal weights.

Hints

  1. Pick a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.
Last updated: Jun 27, 2026

Loading coding console...

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
  • 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.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)