Experiment Bucket Allocation and Rebalancing
Updated
Problem statement
An experiment has bucketCount buckets, numbered 0 through bucketCount - 1. The array groups shows which buckets each group holds right now. groups[0] is the control group, and every later row is an enabled group. A bucket is in at most one group. A bucket that is in no row is currently unallocated, which means no group holds it.
Rebalance the groups so each one ends up with its target share from targetPercentages. The target size of groups[i] is bucketCount * targetPercentages[i] / 100 buckets, and the input guarantees this is a whole number.
- If a group has more buckets than its target size, sort its buckets, keep the smallest ones up to the target size, and release the rest.
- Go through the groups in index order. Give each group that is short of its target the buckets that were unallocated before the rebalance started, smallest first.
- Only when those originally unallocated buckets run out, give it released buckets, smallest first.
Return a two-dimensional array. Its first row lists every bucket that is unallocated after the rebalance. Row i + 1 holds the final buckets of groups[i]. Sort every returned row in ascending order.
Function
rebalanceBuckets(bucketCount: int, groups: int[][], targetPercentages: int[]) → int[][]Examples
Example 1
bucketCount = 20groups = [[0, 1], [2, 3]]targetPercentages = [25, 25]return = [[10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [0, 1, 4, 5, 6], [2, 3, 7, 8, 9]]Each group needs five buckets. The control group fills up first with the smallest unallocated buckets, and then the enabled group takes the next ones. Buckets 10 through 19 stay unallocated, so they form the first row of the answer.
Example 2
bucketCount = 20groups = [[0, 1, 2, 3, 4, 5, 6, 7], [8]]targetPercentages = [25, 25]return = [[5, 6, 7, 13, 14, 15, 16, 17, 18, 19], [0, 1, 2, 3, 4], [8, 9, 10, 11, 12]]The control group has too many buckets. It keeps its smallest buckets and releases 5, 6, and 7. The enabled group fills its gap with buckets 9 through 12, which were unallocated from the start. It never needs the released buckets, so they stay unallocated in the answer.
Example 3
bucketCount = 20groups = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17]]targetPercentages = [25, 50]return = [[7, 8, 9, 10, 11], [0, 1, 2, 3, 4], [5, 6, 12, 13, 14, 15, 16, 17, 18, 19]]The control group keeps its smallest buckets and releases the rest. The enabled group needs more buckets. It first takes 18 and 19, the only buckets that were unallocated from the start. It then takes released buckets 5 and 6. The other released buckets stay unallocated in the answer.
Constraints
1 <= bucketCount <= 10001 <= groups.length == targetPercentages.length <= 200 <= groups[i].length <= bucketCount- Every bucket ID is in
[0, bucketCount - 1]and appears in at most one input row. 0 <= targetPercentages[i] <= 100, their sum is at most100, and every productbucketCount * targetPercentages[i]is divisible by100.
More Pinterest Phone Screen questions
- Shortest Pin Path Through TopicsPHONE SCREEN · Seen Nov 2026
- Top K Pins with Insert and QueryPHONE SCREEN · Seen Nov 2026
- Pins Violation Log QueriesPHONE SCREEN · Seen Nov 2026
- Sparse Matrix Addition and MultiplicationPHONE SCREEN · Seen Nov 2026
- Minimum Bus Routes to a DestinationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delete and Compact a Forest SubtreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Optimal Account BalancingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Aggregate Matching IPv4 Addresses Across ResponsesPHONE SCREEN · Seen Oct 2026
- Escape Room LeaderboardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Shortest Way to Form a Target StringPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Latest Arrival Time for a ShuttleGoogle · ONSITE INTERVIEWGoogle interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Determine the Winner of Three-Card HandsOpenAI · PHONE SCREENOpenAI interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Resource Reconciler: Plan and Apply CreatesPerplexity AI · PHONE SCREENPerplexity AI interview questions
- Deterministic Secret Santa AssignmentShopify · PHONE SCREENShopify interview questions
- Remove the Fewest Magic BeansThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Aggressive CowsAmazon · PHONE SCREEN