← Questions

Random pick weighted by population

0
AppleMetaCodingLast asked Jul 2026

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.

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