Experiment Bucket Allocation and Rebalancing
Learn this problemProblem statement
An experiment owns bucketCount buckets labeled from 0 through bucketCount - 1. The current allocation is given by groups, where groups[0] is the control group and every later row is an enabled group. A bucket appears in at most one group. Any bucket absent from all rows is currently unallocated.
Rebalance every group to its target percentage from targetPercentages. Each target is guaranteed to produce a whole number of buckets: bucketCount * targetPercentages[i] / 100.
- If a group has too many buckets, keep its smallest target-sized prefix after sorting and release the rest.
- Process groups in index order. Fill deficits using buckets that were unallocated before the rebalance, in ascending order.
- Only after those initially unallocated buckets are exhausted, use released buckets in ascending order.
Return a two-dimensional array whose first row contains every bucket left unallocated after the rebalance. Row i + 1 contains the final sorted allocation for groups[i]. Every returned row must be sorted 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 initially unallocated buckets are consumed in ascending order and in group-index order, leaving buckets 10 through 19 unallocated.
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 releases 5, 6, and 7. The enabled group still takes initially unallocated buckets 9 through 12 first, so the released buckets remain unallocated.
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 enabled group first takes initially unallocated buckets 18 and 19, then takes released buckets 5 and 6. The other released buckets remain unallocated.
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 problems
- Nested Set Structural EquivalencePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Nearest Eligible ElevatorPHONE SCREEN · Seen Jul 2026
- Time-Ordered Elevator DispatchPHONE SCREEN · Seen Jul 2026
- Assign Pins to the Shortest ColumnPHONE SCREEN · Seen May 2026
- Minimize Expression with ParenthesesOA · Seen Feb 2026
- Bubble ExplosionOA · Seen Feb 2026
- Travel Distance on ScootersOA · Seen Feb 2026
- Join Sources with Lagged Destination TimestampsONSITE INTERVIEW · Seen Aug 2024