Assign users to overlapping experiment layers with per-layer salted hashing
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `assign_layers(user_id, layer_salts)` for overlapping experiment layers. Each layer has its own salt, so a user's assignment in one layer is independent of their assignment in every other layer. Return the user's variant in each layer.
Requirements:
- For each layer, compute `bucket = int(hashlib.md5(f"{layer_salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100`, using exactly this string format.
- `bucket < 50` maps to `"control"`, and any other bucket maps to `"treatment"`.
- Return a plain `dict` from layer name to variant, keeping the insertion order of `layer_salts`.
- An empty `layer_salts` returns an empty `dict`.
Example:
```python
assign_layers("user_42", {"ranking": "rank_v2", "ui": "ui_v7"})
# -> {"ranking": <variant>, "ui": <variant>}
# each variant is "control" or "treatment", decided by the buckets of
# md5("rank_v2:user_42") and md5("ui_v7:user_42")
assign_layers("user_42", {})
# -> {}
```
```hint Two layers, one user
Think about what the hash input has to contain so that the same user gets unrelated buckets in two layers, and why every service must build that string in exactly the same way.
```
### What a Strong Answer Covers
- An exact implementation of the specified hash string, digest parsing and modulo, so any service reproduces the same bucket.
- Why distinct salts make layer assignments independent, and what breaks if two layers share a salt.
- Deterministic, stateless assignment: the same user always lands in the same variant.
- Order preservation, the empty case and the cost per call.
### Follow-up Questions
- How would you empirically verify orthogonality between two layers?
- How would you support uneven splits or more than two variants per layer?
- Why md5 here, and would you change the hash?
Overview: Implement deterministic assignment of a user to variants across overlapping experiment layers using a per-layer salted MD5 hash. Tests hashing for traffic bucketing, why separate salts keep layers independent, and how to verify that independence empirically.