Compute revenue per assigned user for each A/B test variant in pandas

Quick Overview

Write a pandas function that computes mean revenue per assigned user for each A/B test variant from an assignment table and a transactions table. It tests choosing the right denominator, keeping users who never purchased in the average, and aggregating at the user level before joining.

Compute revenue per assigned user for each A/B test variant in pandas

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `revenue_per_user_by_variant(assignment, transactions)` in `solution.py`. `assignment` has one row per assigned user (`user_id` object, `variant` object, `'control'`/`'treatment'`). `transactions` has zero or more rows per user (`user_id` object, `revenue` float64), and non-purchasers do not appear there. Return mean revenue per assigned user for each variant. Requirements: - Sum revenue per user, then average over ALL assigned users in each variant (the denominator comes from `assignment`, not from purchasers). - Assigned users with no transactions count as `0` (not `NaN`, not omitted). - Return columns `['variant', 'revenue_per_user']`, one row per variant. - Sort by `variant` ascending and reset the index. - Dtypes: `variant` object, `revenue_per_user` float64. Example: ```python assignment = pd.DataFrame({ 'user_id': ['u1', 'u2', 'u3', 'u4'], 'variant': ['control', 'control', 'treatment', 'treatment'], }) transactions = pd.DataFrame({ 'user_id': ['u1', 'u1', 'u3'], 'revenue': [10.0, 5.0, 20.0], }) # u2 and u4 never purchased -> contribute 0 # control: (15 + 0) / 2 = 7.5 # treatment: (20 + 0) / 2 = 10.0 # Output: # variant revenue_per_user # 0 control 7.5 # 1 treatment 10.0 ``` ```hint Row granularity Count how many rows a user with three purchases contributes after your join. Each assigned user should count once in the average. ``` ```hint Which table drives the join Decide which table has to drive the join so that users who never bought anything are still in the result. ``` ### Constraints & Assumptions - Every `user_id` in `transactions` is guaranteed to appear in `assignment`. - `assignment` has exactly one row per user, and `transactions` may be empty. ### What a Strong Answer Covers - Aggregating transactions to one value per user before combining the tables - A join that keeps every assigned user, with non-purchasers filled as zero - Why the denominator is all assigned users and what goes wrong with purchasers only - The exact output contract: column names, sort order, reset index and dtypes - Time and space complexity of the group-by and join ### Follow-up Questions - How would you also return the number of users and total revenue per variant? - How would you add a confidence interval or significance test for the difference between variants? - Revenue per user is right-skewed with a spike at zero. What does that do to the variance of the estimate, and what can you do about it?

Overview: Write a pandas function that computes mean revenue per assigned user for each A/B test variant from an assignment table and a transactions table. It tests choosing the right denominator, keeping users who never purchased in the average, and aggregating at the user level before joining.

Nov 24, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement revenue_per_user_by_variant(assignment, transactions) in solution.py. assignment has one row per assigned user (user_id object, variant object, 'control'/'treatment'). transactions has zero or more rows per user (user_id object, revenue float64), and non-purchasers do not appear there. Return mean revenue per assigned user for each variant.

Requirements:

  • Sum revenue per user, then average over ALL assigned users in each variant (the denominator comes from assignment , not from purchasers).
  • Assigned users with no transactions count as 0 (not NaN , not omitted).
  • Return columns ['variant', 'revenue_per_user'] , one row per variant.
  • Sort by variant ascending and reset the index.
  • Dtypes: variant object, revenue_per_user float64.

Example:

assignment = pd.DataFrame({
    'user_id': ['u1', 'u2', 'u3', 'u4'],
    'variant': ['control', 'control', 'treatment', 'treatment'],
})
transactions = pd.DataFrame({
    'user_id': ['u1', 'u1', 'u3'],
    'revenue': [10.0, 5.0, 20.0],
})
# u2 and u4 never purchased -> contribute 0
# control: (15 + 0) / 2 = 7.5
# treatment: (20 + 0) / 2 = 10.0
# Output:
#      variant  revenue_per_user
# 0    control              7.5
# 1  treatment             10.0

Constraints & Assumptions

  • Every user_id in transactions is guaranteed to appear in assignment .
  • assignment has exactly one row per user, and transactions may be empty.

What a Strong Answer Covers Guidance

  • Aggregating transactions to one value per user before combining the tables
  • A join that keeps every assigned user, with non-purchasers filled as zero
  • Why the denominator is all assigned users and what goes wrong with purchasers only
  • The exact output contract: column names, sort order, reset index and dtypes
  • Time and space complexity of the group-by and join

Follow-up Questions Guidance

  • How would you also return the number of users and total revenue per variant?
  • How would you add a confidence interval or significance test for the difference between variants?
  • Revenue per user is right-skewed with a spike at zero. What does that do to the variance of the estimate, and what can you do about it?