Medium · System Design

Random Pick with Weight

Given an array w of positive integers, implement pickIndex(), which returns a random index i in [0, w.length) with probability w[i] / sum(w).

Examples

Example 1

{
  "weights": [1, 3],
  "targets": [1, 4]
}

Output: [0, 1]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More System Design problems