Random Pick with Weight (LC #528) picks an index proportional to its weight. Build a prefix sum array where prefix[] is the cumulative weight through index . Generate a random number from to total sum. Binary search for the first prefix sum greater than or equal to the random number. Return that index. Preprocessing is , each pick is . Meta asks this "almost every time" as a city populations variant: given city populations, randomly select a city with probability proportional to population.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/meta-interview-prep/sorting-searching/random-pick-with-weight
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████████████████████████████████████████████████████████████████████████████████████████