call(back)
Algorithms & Data Structuresmedium

Rebalance Experiment Buckets with Minimal Reassignment

Pinterest's experiment framework hashes every user into one of n buckets, 0..n-1. Each bucket is assigned to at most one experiment group; unassigned buckets are null. You are given the current assignment and new target sizes:

current = ["A", "A", "A", "B", null, null]      // per-bucket group
targets = { "A": 1, "B": 3 }                    // desired bucket count per group

Produce a new assignment meeting the targets exactly while changing as few buckets as possible — every changed bucket disrupts the experiment for the users hashed into it. Groups present in current but absent from targets lose all their buckets; a bucket going from a group to null counts as a change. You may assume the targets fit: the target counts sum to at most n.

The judge accepts any assignment that meets the targets exactly with the minimum possible number of changed buckets.

Asked at