Back to problems

Random City Name Output Based on Population

Algorithm · Meta · Easy

Problem You are given two parallel arrays: one holds city names and the other holds their corresponding populations. Design a method that returns a random city name such that the probability of selecting a particular city is proportional to its population. For example, suppose ny has 7 million people, la has 8 million, and sf has 5 million. The chance of picking ny should be 7 / (7 + 8 + 5). Input An array of city names: ['ny', 'la', 'sf'] An array of populations: [7, 8, 5]…

Checking your access…