Random pick weighted by population

0
Remix

Given countries and their populations, write a picker that returns a random country with probability proportional to its population. Build prefix sums once, draw a random number up to the total, and binary search for the country it lands in. Discuss the cost per pick, what changes if populations update often, and how you would test that the distribution is right.

AppleMetaTechnical phone screenLast asked Jul 2026
No answer yet. Write the first one; the best answer gets endorsed.