Meta 高频面试题:根据权重随机返回数组下标(Weighted Random Index)

68次阅读
没有评论
// 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

经典的“按权重随机抽样”题:构建前缀和数组,然后生成一个 [1, totalWeight] 的随机数并通过二分查找定位最终下标。Meta 面试中常用于考察概率分布、前缀和和二分搜索的综合能力。

VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Meta、Google、Amazon 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Stripe 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。

正文完
 0