FaceBook | Phone | Container Support Insert & Pop

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2020-08-13Added By Anonymous
Source: Link

Company: Meta (Facebook)
Role: unknown
Interview Stage: Phone Screen
Location: unknown
Year: unknown

Original Post Content:

Design an appropriate data structure that accepts following APIs:

void Insert(Object obj, float weight)
Object RandomPop()

Insert an object with weight, and randomly pops out an object, with the probability proportional to the total weight (i.e. an object with weight 0.5 should have 1/3 probability being popped compared with an object with weight of 1.5; also say total weight of objects we have now is 3, then an object with weight 0.5 should have probability of 0.5/3 being popped).

Comments