Compute the triggered-only conversion rate for each A/B test variant in pandas
Quick Overview
Write a pandas function that returns the conversion rate for each experiment variant using only users who were triggered. It tests filtering before aggregating, why untriggered users dilute an A/B test readout, and returning clean Python types.
Compute the triggered-only conversion rate for each A/B test variant in pandas
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `def triggered_conversion_by_variant(df: pd.DataFrame) -> dict` in `solution.py`. `df` has one row per assigned user with columns `user_id`, `variant`, `triggered` and `converted`. Return the conversion rate per variant among triggered users only.
- Keep only rows where `triggered` is `True`. Untriggered users are excluded from both the numerator and the denominator.
- Group by `variant` and take the mean of `converted`.
- Return a `dict` of `{variant_name: rate}` with `float` rates.
For example:
```text
df:
user_id variant triggered converted
u1 control True 1
u2 control False 0
u3 treatment True 0
u4 treatment True 1
triggered_conversion_by_variant(df)
-> {'control': 1.0, 'treatment': 0.5}
```
User u2 is excluded because it was not triggered, so control's rate is 1 / 1 and treatment's rate is 1 / 2.
```hint Order of operations
Decide whether the filter goes before or after the grouping, and check what each choice does to the denominator of the rate.
```
### Constraints & Assumptions
- One row per assigned user.
- `triggered` is boolean and `converted` is a 0/1 flag.
- A variant with no triggered users has no defined rate and does not appear in the result.
- `pandas` is available.
### What a Strong Answer Covers
- Untriggered users are filtered out before the rate is computed, so they leave both the numerator and the denominator.
- The mean of a 0/1 flag is used as the conversion rate.
- The result is a plain `dict` with variant names as keys and Python floats as values.
- How the function behaves when a variant has no triggered users or `converted` has missing values.
### Follow-up Questions
- Why can including untriggered users bias the treatment effect estimate?
- How would you extend this to also return counts and a confidence interval per variant?
- What would you check before trusting a triggered readout?
Overview: Write a pandas function that returns the conversion rate for each experiment variant using only users who were triggered. It tests filtering before aggregating, why untriggered users dilute an A/B test readout, and returning clean Python types.
Implement def triggered_conversion_by_variant(df: pd.DataFrame) -> dict in solution.py. df has one row per assigned user with columns user_id, variant, triggered and converted. Return the conversion rate per variant among triggered users only.
Keep only rows where
triggered
is
True
. Untriggered users are excluded from both the numerator and the denominator.
Group by
variant
and take the mean of
converted
.
Return a
dict
of
{variant_name: rate}
with
float
rates.
For example:
df:
user_id variant triggered converted
u1 control True 1
u2 control False 0
u3 treatment True 0
u4 treatment True 1
triggered_conversion_by_variant(df)
-> {'control': 1.0, 'treatment': 0.5}
User u2 is excluded because it was not triggered, so control's rate is 1 / 1 and treatment's rate is 1 / 2.
Constraints & Assumptions
One row per assigned user.
triggered
is boolean and
converted
is a 0/1 flag.
A variant with no triggered users has no defined rate and does not appear in the result.
pandas
is available.
What a Strong Answer Covers Guidance
Untriggered users are filtered out before the rate is computed, so they leave both the numerator and the denominator.
The mean of a 0/1 flag is used as the conversion rate.
The result is a plain
dict
with variant names as keys and Python floats as values.
How the function behaves when a variant has no triggered users or
converted
has missing values.
Follow-up Questions Guidance
Why can including untriggered users bias the treatment effect estimate?
How would you extend this to also return counts and a confidence interval per variant?
What would you check before trusting a triggered readout?