Assign experiment variants per session by hashing and flag users who saw both arms
Quick Overview
Write a pandas function that assigns experiment variants per session with a salted md5 hash and flags users whose sessions landed in both arms. It tests deterministic bucketing, broadcasting a per-user result back to every row, exact output dtypes, and the contamination that session-level randomization causes.
Assign experiment variants per session by hashing and flag users who saw both arms
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `def assign_sessions(df: pd.DataFrame, salt: str) -> pd.DataFrame` that assigns a variant per session by hashing `session_id` (not the user) and flags users split across both arms. `df` has columns `"user_id"` (str) and `"session_id"` (str), one row per session, with a RangeIndex. Return a new DataFrame equal to `df` with exactly two appended columns.
The function must meet these requirements:
- `variant = "control" if int(hashlib.md5(f"{salt}:{session_id}".encode("utf-8")).hexdigest(), 16) % 100 < 50 else "treatment"`
- Append `"variant"` (dtype object) then `"user_saw_both"` (dtype bool), in that order.
- `user_saw_both` is `True` if and only if the row's `user_id` has at least one `"control"` and one `"treatment"` session anywhere in `df` (all of a user's rows share the flag).
- Preserve original row order and RangeIndex, and do not mutate `df`.
```hint Per-user facts on per-row data
The flag depends on all of a user's sessions, but it has to appear on every one of that user's rows. Look for a grouped operation that returns one value per original row.
```
### Constraints & Assumptions
- `df` has one row per session and a RangeIndex, which the output keeps.
- `hashlib` and `pandas` are available.
- Assume `salt` is a non-empty string.
### What a Strong Answer Covers
- Hashing `session_id` with the exact recipe, so each session gets a deterministic arm that does not depend on the user.
- Computing `user_saw_both` over every session of the user in the whole frame and broadcasting it to all of that user's rows.
- Appending exactly two columns in the required order and dtypes while keeping row order and index and leaving `df` untouched.
- What session-level assignment does to user-level metrics when one person sees both arms.
### Follow-up Questions
- How would you switch this to user-level randomization, and what changes?
- Why hash with a salt instead of using `random.random()`?
- For a user with k sessions, what share of such users would you expect to be flagged?
Overview: Write a pandas function that assigns experiment variants per session with a salted md5 hash and flags users whose sessions landed in both arms. It tests deterministic bucketing, broadcasting a per-user result back to every row, exact output dtypes, and the contamination that session-level randomization causes.
Implement def assign_sessions(df: pd.DataFrame, salt: str) -> pd.DataFrame that assigns a variant per session by hashing session_id (not the user) and flags users split across both arms. df has columns "user_id" (str) and "session_id" (str), one row per session, with a RangeIndex. Return a new DataFrame equal to df with exactly two appended columns.
Append
"variant"
(dtype object) then
"user_saw_both"
(dtype bool), in that order.
user_saw_both
is
True
if and only if the row's
user_id
has at least one
"control"
and one
"treatment"
session anywhere in
df
(all of a user's rows share the flag).
Preserve original row order and RangeIndex, and do not mutate
df
.
Constraints & Assumptions
df
has one row per session and a RangeIndex, which the output keeps.
hashlib
and
pandas
are available.
Assume
salt
is a non-empty string.
What a Strong Answer Covers Guidance
Hashing
session_id
with the exact recipe, so each session gets a deterministic arm that does not depend on the user.
Computing
user_saw_both
over every session of the user in the whole frame and broadcasting it to all of that user's rows.
Appending exactly two columns in the required order and dtypes while keeping row order and index and leaving
df
untouched.
What session-level assignment does to user-level metrics when one person sees both arms.
Follow-up Questions Guidance
How would you switch this to user-level randomization, and what changes?
Why hash with a salt instead of using
random.random()
?
For a user with k sessions, what share of such users would you expect to be flagged?