Deduplicate experiment exposure logs to each user's first exposure in pandas
Quick Overview
Reduce an experiment exposure log to one row per user by keeping each user's earliest exposure in pandas. The task tests sorting before deduplication, deterministic tie handling with a stable sort, index resets and output column order.
Deduplicate experiment exposure logs to each user's first exposure in pandas
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
An experiment's exposure log records a row every time a user sees a variant, so the same user can appear many times. For first-touch attribution, each user counts once, under the exposure they saw first.
Implement `def dedup_first_exposure(logs: pd.DataFrame) -> pd.DataFrame` in `solution.py`. `logs` has columns `'user'` (int), `'variant'` (str) and `'ts'` (int or float), and a user may appear multiple times. Return one row per user, keeping the earliest exposure (smallest `'ts'`).
Requirements:
- Sort by `'ts'` ascending, then drop duplicate `'user'` rows, keeping the first.
- Sort the result by `'user'` ascending.
- Reset the index with `drop=True` so the result has a clean `RangeIndex`.
- Return the columns `['user', 'variant', 'ts']` in that order.
Example:
```text
Input logs
user variant ts
0 1 treatment 12
1 3 control 2
2 1 control 10
3 2 treatment 5
4 3 control 1
5 3 control 3
Output
user variant ts
0 1 control 10
1 2 treatment 5
2 3 control 1
```
User 1 was seen at ts 10 and 12, so the ts 10 row (control) survives. User 3 was seen at ts 1, 2 and 3, so the ts 1 row survives. The output is sorted by user with the index reset to 0..n-1.
```hint Order before you drop
`drop_duplicates` keeps whichever row it meets first. Think about what the row order has to be at that moment, and whether pandas' default sort keeps that order fixed when two rows share a timestamp.
```
### Clarifying Questions to Ask
- Can `ts` be missing, and should a row with no timestamp ever count as the first exposure?
- Can the same user appear under more than one variant, and should the output flag that?
- Are all timestamps on the same clock and unit (for example, epoch seconds from one service)?
### What a Strong Answer Covers
- Ordering by `ts` before dropping duplicates so the kept row per user is the earliest one
- A deterministic result when a user has tied timestamps, through a stable sort or an explicit secondary key
- The final sort by user, a reset `RangeIndex` and the required column order
- What happens with missing `ts` values and with users who appear under more than one variant
- Time and space complexity
### Follow-up Questions
- How would you keep the latest exposure per user instead?
- How do you break ties when a user has two rows with the same `ts`?
- What does it mean for the analysis when a user's exposures span both variants, and how would you measure how often it happens?
- How would you write the same deduplication for a log too large for pandas, in SQL or Spark?
Overview: Reduce an experiment exposure log to one row per user by keeping each user's earliest exposure in pandas. The task tests sorting before deduplication, deterministic tie handling with a stable sort, index resets and output column order.
An experiment's exposure log records a row every time a user sees a variant, so the same user can appear many times. For first-touch attribution, each user counts once, under the exposure they saw first.
Implement def dedup_first_exposure(logs: pd.DataFrame) -> pd.DataFrame in solution.py. logs has columns 'user' (int), 'variant' (str) and 'ts' (int or float), and a user may appear multiple times. Return one row per user, keeping the earliest exposure (smallest 'ts').
Requirements:
Sort by
'ts'
ascending, then drop duplicate
'user'
rows, keeping the first.
Sort the result by
'user'
ascending.
Reset the index with
drop=True
so the result has a clean
RangeIndex
.
Return the columns
['user', 'variant', 'ts']
in that order.
Example:
Input logs
user variant ts
0 1 treatment 12
1 3 control 2
2 1 control 10
3 2 treatment 5
4 3 control 1
5 3 control 3
Output
user variant ts
0 1 control 10
1 2 treatment 5
2 3 control 1
User 1 was seen at ts 10 and 12, so the ts 10 row (control) survives. User 3 was seen at ts 1, 2 and 3, so the ts 1 row survives. The output is sorted by user with the index reset to 0..n-1.
Clarifying Questions to Ask Guidance
Can
ts
be missing, and should a row with no timestamp ever count as the first exposure?
Can the same user appear under more than one variant, and should the output flag that?
Are all timestamps on the same clock and unit (for example, epoch seconds from one service)?
What a Strong Answer Covers Guidance
Ordering by
ts
before dropping duplicates so the kept row per user is the earliest one
A deterministic result when a user has tied timestamps, through a stable sort or an explicit secondary key
The final sort by user, a reset
RangeIndex
and the required column order
What happens with missing
ts
values and with users who appear under more than one variant
Time and space complexity
Follow-up Questions Guidance
How would you keep the latest exposure per user instead?
How do you break ties when a user has two rows with the same
ts
?
What does it mean for the analysis when a user's exposures span both variants, and how would you measure how often it happens?
How would you write the same deduplication for a log too large for pandas, in SQL or Spark?