 All Problems
Random Pick with Weight
medium
design
math
binary search
prefix sum
google
facebook
lyft

You are given a 0-indexed array of positive integers w where w[i] describes the weight of the ith index.

Implement the Solution class:

  • Solution(int[] w) — initializes the object with the array w.
  • int pickIndex() — returns a random index, weighted proportionally to w[i].

Example:

w = [1, 3] → pickIndex returns 0 with 25% probability, 1 with 75% probability.
Run to check your code against the sample cases, or submit to run every case