Compute a per-variant mean of each user's metric in a window after their own exposure (pandas)
Quick Overview
Write a pandas function that sums each exposed user's event values in a fixed window after their own exposure time and returns the mean per variant of an A/B test. It tests per-user windows, half-open boundaries, keeping zero-event users in the denominator, and the choice of analysis unit.
Compute a per-variant mean of each user's metric in a window after their own exposure (pandas)
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `post_exposure_window_metric(events, exposures, window_days)` with pandas. For each exposed user, sum event `value` within `window_days` days after that user's own exposure. Return the mean per-user sum for each variant.
`exposures` has one row per user with `user_id` (object), `variant` (object, `'control'` or `'treatment'`) and `exposure_time` (datetime64[ns]). `events` has zero or more rows per user with `user_id`, `event_time` (datetime64[ns]) and `value` (float64).
The function must follow these rules:
- Ignore events from users not in `exposures`.
- Keep an event only if `exposure_time <= event_time < exposure_time + window_days days`. The window is half-open, so an event exactly at the window end is excluded.
- Sum the kept `value` per exposed user. A user with no qualifying events contributes `0` and is not dropped.
- Average over all exposed users in each variant, including users with no qualifying events.
- Return `{variant_name (str): mean (float)}` with every variant in `exposures`, even if its mean is `0.0`.
Example:
```text
exposures:
user_id variant exposure_time
u1 treatment 2026-01-01 00:00
u2 treatment 2026-01-05 00:00
u3 control 2026-01-01 00:00
events:
user_id event_time value
u1 2026-01-02 00:00 3.0 # within u1's 7-day window -> kept
u1 2026-01-20 00:00 5.0 # outside window -> dropped
u2 2025-12-31 00:00 9.0 # before exposure -> dropped
u4 2026-01-02 00:00 9.0 # u4 never exposed -> ignored
post_exposure_window_metric(events, exposures, window_days=7)
# treatment: (u1=3.0 + u2=0.0) / 2 = 1.5
# control: (u3=0.0) / 1 = 0.0
# -> {'treatment': 1.5, 'control': 0.0}
```
```hint The denominator
Decide which users belong in the denominator of each variant's mean, then check that no step of your pipeline can silently lose any of them.
```
```hint Window edges
Test an event exactly at a user's exposure_time and another exactly window_days later. Only one of them should count.
```
### Constraints & Assumptions
- `user_id` is unique in `exposures`.
- Timestamps are timezone-naive.
- A user can have zero, one or many rows in `events`, and `events` can contain users who were never exposed.
### What a Strong Answer Covers
- Restricting events to exposed users and to each user's own half-open window.
- Keeping users with no qualifying events in the denominator at a value of 0.
- Returning every variant in `exposures`, including one whose mean is 0.0.
- Why the user, and not the event, is the unit being averaged.
- Time and memory cost of the approach.
### Follow-up Questions
- How would this scale to hundreds of millions of events?
- Why sum per user and then take the mean, instead of a pooled mean over all kept events?
- The analysis runs while users are still being exposed. What happens to users whose window has not closed yet, and how would you handle them?
Overview: Write a pandas function that sums each exposed user's event values in a fixed window after their own exposure time and returns the mean per variant of an A/B test. It tests per-user windows, half-open boundaries, keeping zero-event users in the denominator, and the choice of analysis unit.
Implement post_exposure_window_metric(events, exposures, window_days) with pandas. For each exposed user, sum event value within window_days days after that user's own exposure. Return the mean per-user sum for each variant.
exposures has one row per user with user_id (object), variant (object, 'control' or 'treatment') and exposure_time (datetime64[ns]). events has zero or more rows per user with user_id, event_time (datetime64[ns]) and value (float64).
The function must follow these rules:
Ignore events from users not in
exposures
.
Keep an event only if
exposure_time <= event_time < exposure_time + window_days days
. The window is half-open, so an event exactly at the window end is excluded.
Sum the kept
value
per exposed user. A user with no qualifying events contributes
0
and is not dropped.
Average over all exposed users in each variant, including users with no qualifying events.
Return
{variant_name (str): mean (float)}
with every variant in
exposures
, even if its mean is
0.0
.