Given a list of city names and their corresponding populations, write a program to output a city name
subject to the following constraint: the probability of the program to output a city's name is based on
its population divided by the sum of all cities' population. Assume the program will be repeatedly called many times.
For example:
NY: 7M
SF: 5M
LA: 8M
The probability to generate NY is 7/20, SF is 5/20 and LA 8/20.
This problem is a standard weighted random selection. Build a prefix-sum array based on city populations and map a random value into one of the prefix intervals. This guarantees output probabilities match the weight distribution, and repeated queries remain efficient. The key is prefix sums plus binary search for fast selection.
given a binary search tree, and a integer target, return the node whose value is closest to the target
The BST property allows a greedy search toward the target. At each node, move left or right depending on whether the node value is greater or smaller than the target, while tracking the closest value seen so far. This produces an efficient search path and returns the node whose value is nearest to the target.
The VOprep team has long accompanied candidates through various major company OAs and VOs, including Meta, 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.