KatInterview

Analyzing Noisy A/B Experiment Metrics and SRM in SQL

SQL & DataModel answer
HardModel answerSQL & DataSQLA/B TestingExperimentation

Last updated

Write SQL queries to censor crossover exposures, compute 14-day conversion metrics under blackout constraints, and calculate an SRM chi-square p-value.

You are analyzing an A/B testing experiment (experiment_id = 'tracker_vlaunch_v1'). The target evaluation date is 2025-09-01.

Data Schema

users

  • user_id (INT)
  • country (TEXT)
  • signup_date (DATE)
  • marketing_channel (TEXT)

exposures

  • user_id (INT)
  • experiment_id (TEXT)
  • variant (TEXT)
  • exposure_ts (TIMESTAMP)
  • source (TEXT)

sessions

  • user_id (INT)
  • session_start (TIMESTAMP)
  • device (TEXT)
  • is_bot (BOOLEAN)

orders

  • order_id (INT)
  • user_id (INT)
  • order_ts (TIMESTAMP)
  • revenue (NUMERIC(10,2))
  • refunded (BOOLEAN)

Tasks

Task A: Core Metrics Calculation

Write a single PostgreSQL query for exposures logged between 2025-08-01 and 2025-08-24 (inclusive of the entire day of August 24) for experiment_id = 'tracker_vlaunch_v1'.

Requirements and Business Rules:

  1. Exposure & Crossover Attribution: Assign each user strictly to their earliest exposure variant ever recorded for this experiment. Any subsequent exposures to a different variant (crossovers) must be ignored (censored).
  2. Bot Filtering & Qualification: A user is considered qualified if they have at least one non-bot session (is_bot = false) occurring on or prior to their earliest exposure timestamp (session_start <= exposure_ts). The device used during their earliest qualified session is designated as their qualifying device.
  3. Cohort Scope: Filter the output to include only users residing in US or CA.
  4. Time Window & Blackout Exclusions:
    • Track orders occurring on or after the user's earliest exposure timestamp, strictly within a 14-day window (order_ts < exposure_ts + INTERVAL '14 days'), and strictly prior to 2025-09-01.
    • Exclude any orders placed during an iOS system blackout period defined as ['2025-08-10 00:00:00', '2025-08-12 00:00:00') if the user's qualifying device was ios.
  5. Output Schema: Group by variant and country and return:
    • variant
    • country
    • users_exposed: Total count of exposed users in this country.
    • qualified_users: Total count of qualified non-bot users.
    • converters_14d: Count of qualified users who placed at least one eligible order in the 14-day window.
    • conv_rate_14d: converters_14d / qualified_users (rounded to 4 decimal places).
    • revenue_14d: Total revenue from non-refunded eligible orders placed by qualified users in the window.
    • refund_rate_14d: Total refunded eligible orders divided by total eligible orders placed by qualified users in the window (rounded to 4 decimal places).

Task B: Sample Ratio Mismatch (SRM) Test

Write a second SQL query to perform a 50/50 Chi-Square Goodness-of-Fit test for Sample Ratio Mismatch (SRM) on qualified users across control and B variants.

Requirements:

  • Calculate observed_count_control, observed_count_B, expected_each, chi2_stat, and p_value.
  • Use an accurate approximation for the chi-square CDF/p-value (such as the Abramowitz & Stegun approximation for the error function) using standard mathematical functions (EXP, POWER, LN, SQRT) without external extensions or custom UDFs.

Hints

Hint 1

Find each user's absolute earliest exposure timestamp across the whole table before filtering by the launch date range; otherwise, cross-over users exposed earlier in July might be wrongly counted as first exposed in August.

Hint 2

Aggregate orders per user first before rolling up to the variant-country level to ensure user-level conversion metrics are distinct.

Hint 3

For the 1-DOF chi-square p-value, recall that chi2 with 1 degree of freedom relates to the standard normal distribution via Z = sqrt(chi2), making p = erfc(Z / sqrt(2)).