Random pick with weight

0
Remix

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.

MetaCodingLast asked Oct 2025
No answer yet. Write the first one; the best answer gets endorsed.