Assign users to at most one experiment in a mutual exclusion group with hash bucketing

Quick Overview

Implement a Python function that hashes each user into a shared pool of 100 buckets and assigns them to at most one experiment in a mutual exclusion group, plus a control or treatment variant. It tests deterministic hashing, cumulative traffic ranges, boundary handling and how salts keep placement and variant independent.

Assign users to at most one experiment in a mutual exclusion group with hash bucketing

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `assign_exclusive_group(user_id, group_salt, experiments)` for a mutual exclusion group, where each user enters at most one experiment. Each entry in `experiments` is a tuple `(exp_name, exp_salt, traffic_pct)`. Return the tuple `(exp_name, variant)`. The function must follow these rules: - Raise `ValueError` if the `traffic_pct` integers sum to more than `100`. - Pick the experiment from the group bucket `g = int(hashlib.md5(f"{group_salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100`. - Walk the list in order, accumulating `traffic_pct`. The first experiment whose cumulative total is strictly greater than `g` owns the user, so experiment i owns the range `[cum_{i-1}, cum_i)`. - If `g >= sum(traffic_pct)`, return `(None, None)`. - Pick the variant: `"control"` if `int(hashlib.md5(f"{exp_salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100 < 50`, else `"treatment"`. Example. The bucket values below are illustrative; in a real call they come from the two hashes. ```python experiments = [("exp_a", "salt_a", 30), ("exp_b", "salt_b", 30)] # cumulative ranges: exp_a -> [0, 30), exp_b -> [30, 60), no experiment -> [60, 100) # a user whose group bucket g is 45 is owned by exp_b # if that user's variant bucket under "salt_b" is 12 (< 50), the result is ("exp_b", "control") # a user whose group bucket g is 80 has g >= 60, so the result is (None, None) ``` ```hint Picture the pool Treat the 100 group buckets as one line that the experiments cut into back-to-back pieces in list order. Ask which piece a given bucket falls in. ``` ```hint Check the edges Trace a bucket that sits exactly on a cumulative boundary, such as g = 30 in the example, and an experiment whose traffic_pct is 0. ``` ### Constraints & Assumptions - `traffic_pct` values are integers. Their sum can be anything from 0 to 100; above 100 is an error. - Buckets run from 0 to 99, so one point of `traffic_pct` is one bucket, about 1% of users. - Assignment must be deterministic: the same `user_id` and salts give the same result on every call, process and machine. ### What a Strong Answer Covers - Validation of the total before any user is placed, with `ValueError` on over-allocation. - Mapping the group bucket to exactly one half-open cumulative range, including boundary buckets and the unallocated remainder. - Choosing the variant with the experiment's own salt, and why placement and variant need different salts. - Why a stable hash such as MD5 is required for assignment. - Time and space cost of the function. ### Follow-up Questions - Why hash with MD5 here instead of Python's built-in `hash()`? - How would you support more than two variants with custom split weights? - A user was in an experiment and you now lower its `traffic_pct`. What happens to their assignment, and how would you keep enrolled users stable?

Overview: Implement a Python function that hashes each user into a shared pool of 100 buckets and assigns them to at most one experiment in a mutual exclusion group, plus a control or treatment variant. It tests deterministic hashing, cumulative traffic ranges, boundary handling and how salts keep placement and variant independent.

Nov 24, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement assign_exclusive_group(user_id, group_salt, experiments) for a mutual exclusion group, where each user enters at most one experiment. Each entry in experiments is a tuple (exp_name, exp_salt, traffic_pct). Return the tuple (exp_name, variant).

The function must follow these rules:

  • Raise ValueError if the traffic_pct integers sum to more than 100 .
  • Pick the experiment from the group bucket g = int(hashlib.md5(f"{group_salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100 .
  • Walk the list in order, accumulating traffic_pct . The first experiment whose cumulative total is strictly greater than g owns the user, so experiment i owns the range [cum_{i-1}, cum_i) .
  • If g >= sum(traffic_pct) , return (None, None) .
  • Pick the variant: "control" if int(hashlib.md5(f"{exp_salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100 < 50 , else "treatment" .

Example. The bucket values below are illustrative; in a real call they come from the two hashes.

experiments = [("exp_a", "salt_a", 30), ("exp_b", "salt_b", 30)]
# cumulative ranges: exp_a -> [0, 30), exp_b -> [30, 60), no experiment -> [60, 100)

# a user whose group bucket g is 45 is owned by exp_b
# if that user's variant bucket under "salt_b" is 12 (< 50), the result is ("exp_b", "control")

# a user whose group bucket g is 80 has g >= 60, so the result is (None, None)

Constraints & Assumptions

  • traffic_pct values are integers. Their sum can be anything from 0 to 100; above 100 is an error.
  • Buckets run from 0 to 99, so one point of traffic_pct is one bucket, about 1% of users.
  • Assignment must be deterministic: the same user_id and salts give the same result on every call, process and machine.

What a Strong Answer Covers Guidance

  • Validation of the total before any user is placed, with ValueError on over-allocation.
  • Mapping the group bucket to exactly one half-open cumulative range, including boundary buckets and the unallocated remainder.
  • Choosing the variant with the experiment's own salt, and why placement and variant need different salts.
  • Why a stable hash such as MD5 is required for assignment.
  • Time and space cost of the function.

Follow-up Questions Guidance

  • Why hash with MD5 here instead of Python's built-in hash() ?
  • How would you support more than two variants with custom split weights?
  • A user was in an experiment and you now lower its traffic_pct . What happens to their assignment, and how would you keep enrolled users stable?