Vectorized A/B Bucket Assignment Returning a pandas Series
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `def assign_many(user_ids: list[str], salt: str) -> pd.Series` that assigns a list of users to a 50/50 A/B test. For each id, `bucket = int(hashlib.md5(f"{salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100`, mapping `bucket < 50` to `"control"` and else `"treatment"`.
Requirements:
- Return a pandas `Series` indexed by `user_ids` in the given order, index name `"user_id"`.
- Series name `"variant"`, dtype `object`.
- Do not sort, deduplicate, or reorder the input.
- Empty `user_ids` returns an empty Series with the same name, index name, and dtype `object`.
Example checks that any correct result passes:
```python
s = assign_many(["u1", "u2", "u1"], salt="exp")
list(s.index) # ['u1', 'u2', 'u1'] (input order, duplicate kept)
s.name, s.index.name, s.dtype # ('variant', 'user_id', dtype('O'))
s.iloc[0] == s.iloc[2] # True (the same id always gets the same variant)
set(s) <= {"control", "treatment"} # True
e = assign_many([], salt="exp")
len(e), e.name, e.index.name, e.dtype # (0, 'variant', 'user_id', dtype('O'))
```
A bucket of 49 maps to `"control"` and a bucket of 50 maps to `"treatment"`.
```hint Test the empty case
Call your function with an empty list and print the dtype, name and index name. A Series built from nothing does not always carry the schema you asked for.
```
### What a Strong Answer Covers
- Applies the exact hashing rule to each id and maps buckets 0-49 to control and 50-99 to treatment.
- Builds the Series with the ids as its index in input order, duplicates included, with index name `"user_id"` and name `"variant"`.
- Returns the same name, index name and dtype for empty and non-empty input.
- Discusses the time and space cost and whether the hashing step can be vectorized.
### Follow-up Questions
- How would you generalize this to unequal splits (for example 70/30)?
- Why hash the id instead of using a random assignment?
- How would you assign 100 million ids quickly while keeping every result identical to this function?
Overview: Implement a function that assigns a list of user ids to control or treatment by salted MD5 bucketing and returns a labeled pandas Series. It tests applying a fixed hashing rule, preserving input order and duplicates, and keeping the output schema stable for empty input.