← Questions
Random pick weighted by population
0
I was asked this
No reports yet. Sign in to say where you were asked it.
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.
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Write and test your solution herePython in your browser, the question's tests level by level, saved for you.Sign in to start coding

