Assign a user to a weighted experiment variant with salted-hash bucketing
Quick Overview
Write a function that deterministically assigns a user to one of several weighted experiment arms using a salted MD5 bucket and cumulative traffic ranges. It tests half-open boundaries, zero-weight variants, input validation, and why hash-based assignment is the usual choice for A/B tests.
Assign a user to a weighted experiment variant with salted-hash bucketing
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `assign_weighted(user_id, salt, weights)` that deterministically assigns a user to one of two or more arms. `weights` maps each variant name to an integer traffic percentage. Variant i owns buckets `[cum_{i-1}, cum_i)`.
Requirements:
- Raise `ValueError` if the weights do not sum to exactly `100`.
- Compute the bucket as `bucket = int(hashlib.md5(f"{salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100`.
- Accumulate in insertion order and return the first variant whose cumulative total is strictly greater than `bucket`.
- A weight-`0` variant owns no buckets and is never returned.
- Return a plain Python `str`.
```python
weights = {"control": 90, "treatment": 10}
# buckets 0-89 -> "control"
# buckets 90-99 -> "treatment"
assign_weighted("user_42", "exp1", weights) # -> "control" or "treatment"
assign_weighted("user_42", "exp1", {"off": 0, "on": 100}) # -> "on", for every user
assign_weighted("user_42", "exp1", {"control": 50, "treatment": 40}) # raises ValueError
```
```hint Order inside the loop
Decide whether a variant's weight is added to the running total before or after the comparison with the bucket, then trace a weight-0 variant at the front of the dict through your loop.
```
### Constraints & Assumptions
- Buckets are integers from 0 to 99.
- Weights are integer percentages that must sum to exactly 100.
- `weights` preserves insertion order (Python 3.7+ dicts).
### What a Strong Answer Covers
- Validating the weight sum and raising `ValueError` before any assignment happens.
- Computing the bucket with the exact expression so the assignment is stable across calls and machines.
- Walking cumulative totals in insertion order with a strict comparison, and why that produces half-open ranges.
- Why a weight-0 variant can never be returned, and the return type.
- Time and space cost per call.
### Follow-up Questions
- Why use a hash of `salt:user_id` instead of `random`?
- How would you ramp treatment from 10% to 50% without reshuffling existing users?
- Is `md5 % 100` uniform enough, and how would you test it?
Overview: Write a function that deterministically assigns a user to one of several weighted experiment arms using a salted MD5 bucket and cumulative traffic ranges. It tests half-open boundaries, zero-weight variants, input validation, and why hash-based assignment is the usual choice for A/B tests.
Implement assign_weighted(user_id, salt, weights) that deterministically assigns a user to one of two or more arms. weights maps each variant name to an integer traffic percentage. Variant i owns buckets [cum_{i-1}, cum_i).
Requirements:
Raise
ValueError
if the weights do not sum to exactly
100
.
Compute the bucket as
bucket = int(hashlib.md5(f"{salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100
.
Accumulate in insertion order and return the first variant whose cumulative total is strictly greater than
bucket
.
A weight-
0
variant owns no buckets and is never returned.