Microsoft SDE Real Interview Question: Generate a Random City Name Based on Population Weights

14 Views
No Comments
// City Generator
// Given a list of city names and their corresponding populations, write a function to output a
// random city name subject to the following constraint: the probability of the function to output a
// city's name is based on its population divided by the sum of all cities' population.

// For example:
// NY: 7MM
// SF: 5MM
// LA: 8MM

// The probability to generate NY is 7/20, SF is 1/4.

This Microsoft interview problem focuses on weighted random selection. Given cities and their populations, the function should return a city name with probability proportional to its population. A common solution uses a prefix-sum array and binary search to map a random number to the correct weighted interval.

The VOprep team has long accompanied candidates through various major company OAs and VOs, including Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for these companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.

END
 0