I am working on below interview question:
If you're given a list of countries and its corresponding population, write a function that will return a random country but the higher the population of the country, the more likely it is to be picked at random.
I came up with below logic:
Make two traversal on the list. In the first traversal, sum up all the population of every location to get the total population TOTAL_POP. In the second iteration, calculate % of each location's population against TOTAL_POP. For example, if location A has a popoulation 'a'. The percentage of population for A is (a/TOTAL_POP)*100.
Let's say, after these steps, we have the following values. Location A = 35% B = 22% C = 19% D = 20% E = 4%
Note that the percentages should add up to 100.
Now, randomly generate a number 'n' between 1 and 100.
if 1 <= n <=35 OUTPUT A 36 <= n <= 57 OUTPUT B
Is there any better way to solve this problem? Algorithm or code anything is fine. Also if we are gonna implement this in Java then what is the best data structure to use here?