Detect contaminated users in an experiment exposure log with pandas
Quick Overview
Write a pandas function that finds users who appear under more than one experiment variant in an exposure log and returns their ids in ascending order. It tests group-by aggregation, the difference between duplicate exposures and real contamination, and how to treat such users in an A/B test readout.
Detect contaminated users in an experiment exposure log with pandas
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Given an exposure log as a `pandas` DataFrame with columns `'user'` (int) and `'variant'` (str), implement `def contaminated_users(logs: pd.DataFrame) -> list[int]` in `solution.py`. Return the user ids seen under 2 or more distinct variants, sorted ascending.
Requirements:
- Flag users whose distinct variant count is `> 1`.
- Repeated rows with the same variant are not contamination.
- Return native Python `int` values, sorted ascending.
Example:
```python
# user variant
# 1 A
# 1 A
# 3 A
# 3 B
contaminated_users(logs) # -> [3]
```
User 1 has two rows but only one variant, so only user 3 is returned.
```hint Rows versus variants
Two rows for the same user do not mean two variants. Pick an aggregation that ignores repeats of the same value.
```
### Constraints & Assumptions
- `logs` has one row per exposure event, so a user can appear many times.
- Column `'user'` holds integer ids and column `'variant'` holds variant labels as strings.
- The log can be empty, in which case the result is an empty list.
### What a Strong Answer Covers
- Grouping by user and counting distinct variants, so repeated exposures under one variant are not flagged.
- Filtering to counts above one and returning native Python ints in ascending order.
- A vectorized aggregation that runs in linear time, with no Python loop over rows.
- Edge cases such as an empty log or a missing variant value.
### Follow-up Questions
- How would you extend this to also report the offending variants per user?
- Once contaminated users are identified, how do you handle them in the analysis?
- What usually causes contamination, and how would you tell a bucketing bug from a logging bug?
Overview: Write a pandas function that finds users who appear under more than one experiment variant in an exposure log and returns their ids in ascending order. It tests group-by aggregation, the difference between duplicate exposures and real contamination, and how to treat such users in an A/B test readout.
Given an exposure log as a pandas DataFrame with columns 'user' (int) and 'variant' (str), implement def contaminated_users(logs: pd.DataFrame) -> list[int] in solution.py. Return the user ids seen under 2 or more distinct variants, sorted ascending.
Requirements:
Flag users whose distinct variant count is
> 1
.
Repeated rows with the same variant are not contamination.
Return native Python
int
values, sorted ascending.
Example:
# user variant
# 1 A
# 1 A
# 3 A
# 3 B
contaminated_users(logs) # -> [3]
User 1 has two rows but only one variant, so only user 3 is returned.
Constraints & Assumptions
logs
has one row per exposure event, so a user can appear many times.
Column
'user'
holds integer ids and column
'variant'
holds variant labels as strings.
The log can be empty, in which case the result is an empty list.
What a Strong Answer Covers Guidance
Grouping by user and counting distinct variants, so repeated exposures under one variant are not flagged.
Filtering to counts above one and returning native Python ints in ascending order.
A vectorized aggregation that runs in linear time, with no Python loop over rows.
Edge cases such as an empty log or a missing variant value.
Follow-up Questions Guidance
How would you extend this to also report the offending variants per user?
Once contaminated users are identified, how do you handle them in the analysis?
What usually causes contamination, and how would you tell a bucketing bug from a logging bug?