Pick an index with probability proportional to its weight

Quick Overview

Given an array of positive weights and a simulated uniform draw, return the index whose weighted slot contains the scaled draw. Tests prefix sums, upper-bound binary search and careful handling of slot boundaries in weighted random sampling.

Pick an index with probability proportional to its weight

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a 0-indexed array of positive integers `w`, where `w[i]` is the weight of index `i`. Implement `pick_index`, which picks an index in the range `[0, len(w) - 1]` (inclusive). The probability of picking index `i` is `w[i] / sum(w)`. For deterministic testing, the function takes an additional parameter `target` that simulates a uniform random draw in `[0, 1)`. Let `S = sum(w)` and `prefix[i] = w[0] + w[1] + ... + w[i]`. Return the smallest index `i` such that `target * S < prefix[i]`. ### Function Signature ```python def pick_index(w: list[int], target: str) -> int: ``` ### Rules - `target` is passed as a decimal string, for example `"0.5"`. Convert it to a floating-point number before using it. - Compute `x = target * S` in double-precision floating point (in Python, `float(target) * S`) and compare it with the integer prefix sums. - The comparison is strict. When `x` equals `prefix[i]` exactly, index `i` does not qualify and the answer is a later index. ### Constraints - `1 <= len(w) <= 10^4` - `1 <= w[i] <= 10^5` - `0 <= target < 1` ### Examples ```text Input: w = [1, 3], target = "0.5" Output: 1 ``` The total is 4 and the prefix sums are `[1, 4]`, so `x = 0.5 * 4 = 2.0`. The first prefix sum greater than 2.0 is 4, at index 1. ```text Input: w = [1, 3], target = "0.1" Output: 0 ``` Here `x = 0.1 * 4 = 0.4`, and the first prefix sum, 1, is already greater than 0.4. ```text Input: w = [2, 3, 5], target = "0.5" Output: 2 ``` The total is 10 and the prefix sums are `[2, 5, 10]`, so `x = 5.0`. Index 1 has prefix sum 5, which is not strictly greater than 5.0, so the answer is index 2.

Overview: Given an array of positive weights and a simulated uniform draw, return the index whose weighted slot contains the scaled draw. Tests prefix sums, upper-bound binary search and careful handling of slot boundaries in weighted random sampling.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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

Implement pick_index, which picks an index in the range [0, len(w) - 1] (inclusive). The probability of picking index i is w[i] / sum(w).

For deterministic testing, the function takes an additional parameter target that simulates a uniform random draw in [0, 1). Let S = sum(w) and prefix[i] = w[0] + w[1] + ... + w[i]. Return the smallest index i such that target * S < prefix[i].

Function Signature

def pick_index(w: list[int], target: str) -> int:

Rules

  • target is passed as a decimal string, for example "0.5" . Convert it to a floating-point number before using it.
  • Compute x = target * S in double-precision floating point (in Python, float(target) * S ) and compare it with the integer prefix sums.
  • The comparison is strict. When x equals prefix[i] exactly, index i does not qualify and the answer is a later index.

Constraints

  • 1 <= len(w) <= 10^4
  • 1 <= w[i] <= 10^5
  • 0 <= target < 1

Examples

Input: w = [1, 3], target = "0.5"
Output: 1

The total is 4 and the prefix sums are [1, 4], so x = 0.5 * 4 = 2.0. The first prefix sum greater than 2.0 is 4, at index 1.

Input: w = [1, 3], target = "0.1"
Output: 0

Here x = 0.1 * 4 = 0.4, and the first prefix sum, 1, is already greater than 0.4.

Input: w = [2, 3, 5], target = "0.5"
Output: 2

The total is 10 and the prefix sums are [2, 5, 10], so x = 5.0. Index 1 has prefix sum 5, which is not strictly greater than 5.0, so the answer is index 2.