cswork / libraryOpen practice workspace

Coding questions · OpenAI

Compute signup rate and retention from raw logs

This question evaluates the ability to manipulate event-level logs to compute cohort metrics—specifically signup rate and D30 retention—testing proficiency in SQL/Python, event-time windowing, joins, aggregation, and understanding of upstream logging semantics.

Scenario

You are analyzing an A/B test for a marketing campaign offering a free 1-month trial.

You are given raw “upstream” tables that resemble production event logs rather than pre-aggregated metrics.

Assume UTC timestamps and that each user has at most one assignment.

Tables (upstream)

experiment_assignment

  • user_id (STRING, PK)
  • experiment_id (STRING)
  • variant (STRING; 'control' or 'treatment')
  • assigned_at (TIMESTAMP)

offer_exposure_events

  • user_id (STRING)
  • exposed_at (TIMESTAMP)
  • campaign_id (STRING)

subscription_events

  • user_id (STRING)
  • event_time (TIMESTAMP)
  • event_type (STRING; one of 'trial_start', 'paid_start', 'cancel')

app_sessions

  • user_id (STRING)
  • session_start (TIMESTAMP)

Metric definitions

  1. Signup rate (trial start within 7 days)
  • Denominator: users assigned to the experiment.
  • Numerator: users with a trial_start event where event_time is in [assigned_at, assigned_at + 7 days).
  1. D30 activity retention Among users who started a trial within 7 days, the user is retained if they have ≥ 1 app session with session_start in [assigned_at + 30 days, assigned_at + 37 days).

Task

Write SQL to output one row per variant with:

  • variant
  • assigned_users
  • signup_users_7d
  • signup_rate_7d
  • retained_users_d30
  • retention_rate_d30

Also briefly describe what an “upstream” dataset/logging pipeline must contain to compute these metrics reliably (e.g., assignment logs, exposure logs, subscription lifecycle events, identity consistency).

A/B test: compute signup rate and 7-day retention from raw event logs

You are analyzing an A/B test for a marketing campaign that offers users a free month trial.

Definitions (use only the tables below):

  • The experiment assignment population includes users assigned between 2025-05-01 and 2025-05-31 (inclusive).
  • A user is considered "signed up" if they have at least one signup event in event_log between 2025-05-01 and 2025-05-31 (inclusive).
  • Signup rate = (# signed up users) / (# assigned users), computed per variant.
  • 7-day retention is computed on a signup cohort: users who signed up between 2025-05-01 and 2025-05-24 (inclusive).
  • A signed-up user is "7-day retained" if they have at least one app_open event on the calendar date exactly 7 days after their signup date.
  • 7-day retention rate = (# 7-day retained users) / (# eligible signed-up users), computed per variant.

Task: Write a SQL query that returns one row per variant with:

  • assigned_users
  • signed_up_users
  • signup_rate
  • eligible_signup_users_7d
  • retained_users_7d
  • retention_rate_7d

Notes:

  • If a user has multiple signup events, use the earliest signup date.
  • Rates should be returned as decimals rounded to 4 decimal places.

Table experiment_assignment

columntypeconstraints
user_idINTPRIMARY KEY, NOT NULL
variantVARCHAR(20)NOT NULL
assigned_atDATENOT NULL

Sample data:

assigned_atuser_idvariant
2025-05-011control
2025-05-032control
2025-05-103control
2025-05-204control
2025-05-255control
2025-05-306control
2025-05-027treatment
2025-05-058treatment
2025-05-129treatment
2025-05-1810treatment
2025-05-2211treatment
2025-05-2812treatment

Table event_log

columntypeconstraints
event_idINTPRIMARY KEY, NOT NULL
user_idINTNOT NULL
event_typeVARCHAR(20)NOT NULL
event_tsTIMESTAMPNOT NULL

Sample data:

event_idevent_tsevent_typeuser_id
1012025-05-02 09:15:00signup1
1022025-05-09 08:00:00app_open1
1032025-05-10 10:00:00app_open1
1042025-05-11 11:30:00signup3
1052025-05-18 07:45:00app_open3
1062025-05-21 14:10:00signup4
1072025-05-28 09:00:00app_open4
1082025-05-26 16:20:00signup5
1092025-05-27 09:00:00app_open5
1102025-05-03 10:00:00signup7
1112025-05-10 12:00:00app_open7
1122025-05-06 09:30:00signup8
1132025-05-12 09:00:00app_open8
1142025-05-13 18:00:00app_open8
1152025-05-19 13:00:00signup10
1162025-05-26 08:30:00app_open10
1172025-05-23 20:15:00signup11
1182025-05-24 10:10:00app_open11
1192025-05-29 09:05:00signup12
1202025-05-12 10:00:00signup3

Expected output

One row per variant with assignment count, signup count and signup rate for 2025-05-01 to 2025-05-31, plus eligible signup cohort size for 7-day retention (signups through 2025-05-24), retained users (app_open exactly 7 days after signup date), and 7-day retention rate. Rates are rounded to 4 decimals.

assigned_userseligible_signup_users_7dretained_users_7dretention_rate_7dsigned_up_userssignup_ratevariant
633140.6667control
6430.7550.8333treatment

Hints

  1. Deduplicate signups by taking the earliest signup date per user.
  2. Compute 7-day retention using date arithmetic: app_open date = signup_date + 7 days.
  3. Be careful to restrict the retention cohort to signups on or before 2025-05-24 so 7 days is observable by 2025-06-01.