Back to problems

Implement weighted random city and sparse dot product

Algorithm · Meta · Medium

Question 1 Design a CityPicker class that chooses a city randomly, where every city's chance of being selected is proportional to its provided weight. The constructor receives two parallel arrays: cities[i] is a city name and weights[i] is the relative weight of that city. The pickCity() method returns one of the strings from cities. For each index i, the probability that pickCity() returns cities[i] is: $$ \frac{weights[i]}{\sum_j weights[j]} $$ Every weight is positive,…

Checking your access…