Meta Interview Question: Pick a Random Index Based on Weights

13 Views
No Comments
// Given an array of positive integers, where each value within the array represents a
// weight of the corresponding index, implement a structure which can be invoked multiple times
// to pick a random index within that array according to in proportion to its weight.
//
// array [1, 7, 2]
// In 10 times, 0 -- 1 time, 1 -- 7 times, 2 -- 2 times

A classic weighted random selection question: build a prefix-sum array, generate a random number within the weighted range, and use binary search to find the correct index. Commonly tested in Meta interviews for probability distribution and prefix-sum logic

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.

END
 0