← Questions

Random pick with weight

0
MetaCodingLast asked Oct 2025

Given positive weights, implement pick() that returns index i with probability w[i] / sum(w). Build prefix sums once, then binary search a random number into them. One version gives cities with their populations and asks for a random city chosen in proportion to its population.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding