Flag a sample ratio mismatch in a 50/50 A/B test with a chi-square statistic in SQL

Quick Overview

Write a PostgreSQL query that checks a 50/50 A/B test for sample ratio mismatch by computing a one degree of freedom chi-square statistic and comparing it with a value from a critical-value lookup table. It tests counting per arm when an arm can be empty, decimal division and threshold logic.

Flag a sample ratio mismatch in a 50/50 A/B test with a chi-square statistic in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `assignments` table has one row per user in an A/B test, holding the `variant` the user was assigned to (`'control'` or `'treatment'`). The test was designed as a 50/50 split. Check it for a sample ratio mismatch (SRM) by computing the chi-square goodness-of-fit statistic with one degree of freedom: $$ \chi^2 = \sum_{v \in \{\text{control},\ \text{treatment}\}} \frac{(O_v - E_v)^2}{E_v} $$ Here $O_v$ is the number of users in variant $v$, and each expected count is $E_v = N / 2$, where $N$ is the total number of users. Return a single row with `chi_square` (the statistic rounded to 4 decimals), `critical_value` (read from the `critical_values` row with `df = 1` and `alpha = 0.05`), and `srm_flag`, which is 1 when the statistic is greater than the critical value and 0 otherwise. ```hint Odd totals In PostgreSQL, `COUNT(*) / 2` is integer division. With an odd number of users the expected count has to keep its half. ``` ### Input Table(s) Table `assignments`: | Column | Type | Meaning | |---|---|---| | `variant` | VARCHAR(20) | Arm the user was assigned to: `'control'` or `'treatment'` | One row per user. The table has no other columns and no key. Table `critical_values`: | Column | Type | Meaning | |---|---|---| | `df` | INT | Degrees of freedom | | `alpha` | DECIMAL(4,3) | Significance level | | `critical_value` | DECIMAL(8,4) | Upper critical value of the chi-square distribution for that `df` and `alpha` | Each (`df`, `alpha`) pair appears at most once. ### Output Contract - Exactly one row with the columns `chi_square`, `critical_value`, `srm_flag`, in that order. - `chi_square` is the statistic rounded to 4 decimal places with `ROUND(..., 4)`. - `critical_value` is the value stored in the lookup row, unchanged. - `srm_flag` is the integer 1 or 0. - The result is a single row, so no ordering applies. ### Example Input `assignments` (19 rows): | variant | |---| | treatment | | control | | control | | control | | treatment | | control | | treatment | | control | | treatment | | treatment | | treatment | | treatment | | control | | control | | treatment | | control | | control | | treatment | | control | Input `critical_values`: | df | alpha | critical_value | |---|---|---| | 1 | 0.050 | 3.8410 | | 2 | 0.050 | 5.9910 | | 3 | 0.050 | 7.8150 | Expected output: | chi_square | critical_value | srm_flag | |---|---|---| | 0.0526 | 3.8410 | 0 | There are 10 control users and 9 treatment users, so $N = 19$ and each expected count is 9.5. Both arms are 0.5 away from 9.5, so the statistic is ${2 \cdot 0.25 / 9.5}$, about 0.052632. That is far below 3.841, so the split shows no mismatch. ### Constraints and Clarifications - `assignments` has at least one row. `variant` is never NULL and is always exactly `'control'` or `'treatment'`. - An arm can have zero users. It still contributes $(0 - N/2)^2 / (N/2)$ to the statistic. - `critical_values` has exactly one row with `df = 1` and `alpha = 0.05`. It can also hold rows for other degrees of freedom and other significance levels. - Compare the unrounded statistic with the critical value. A statistic exactly equal to the critical value gives `srm_flag = 0`. - Use the plain statistic above, with no continuity correction.

Overview: Write a PostgreSQL query that checks a 50/50 A/B test for sample ratio mismatch by computing a one degree of freedom chi-square statistic and comparing it with a value from a critical-value lookup table. It tests counting per arm when an arm can be empty, decimal division and threshold logic.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

The assignments table has one row per user in an A/B test, holding the variant the user was assigned to ('control' or 'treatment'). The test was designed as a 50/50 split. Check it for a sample ratio mismatch (SRM) by computing the chi-square goodness-of-fit statistic with one degree of freedom:

χ2=∑v∈{control, treatment}(Ov−Ev)2Ev\chi^2 = \sum_{v \in \{\text{control},\ \text{treatment}\}} \frac{(O_v - E_v)^2}{E_v}

Here OvO_v is the number of users in variant vv, and each expected count is Ev=N/2E_v = N / 2, where NN is the total number of users. Return a single row with chi_square (the statistic rounded to 4 decimals), critical_value (read from the critical_values row with df = 1 and alpha = 0.05), and srm_flag, which is 1 when the statistic is greater than the critical value and 0 otherwise.

Input Table(s)

Table assignments:

ColumnTypeMeaning
variantVARCHAR(20)Arm the user was assigned to: 'control' or 'treatment'

One row per user. The table has no other columns and no key.

Table critical_values:

ColumnTypeMeaning
dfINTDegrees of freedom
alphaDECIMAL(4,3)Significance level
critical_valueDECIMAL(8,4)Upper critical value of the chi-square distribution for that df and alpha

Each (df, alpha) pair appears at most once.

Output Contract

  • Exactly one row with the columns chi_square , critical_value , srm_flag , in that order.
  • chi_square is the statistic rounded to 4 decimal places with ROUND(..., 4) .
  • critical_value is the value stored in the lookup row, unchanged.
  • srm_flag is the integer 1 or 0.
  • The result is a single row, so no ordering applies.

Example

Input assignments (19 rows):

variant
treatment
control
control
control
treatment
control
treatment
control
treatment
treatment
treatment
treatment
control
control
treatment
control
control
treatment
control

Input critical_values:

dfalphacritical_value
10.0503.8410
20.0505.9910
30.0507.8150

Expected output:

chi_squarecritical_valuesrm_flag
0.05263.84100

There are 10 control users and 9 treatment users, so N=19N = 19 and each expected count is 9.5. Both arms are 0.5 away from 9.5, so the statistic is 2⋅0.25/9.5{2 \cdot 0.25 / 9.5}, about 0.052632. That is far below 3.841, so the split shows no mismatch.

Constraints and Clarifications

  • assignments has at least one row. variant is never NULL and is always exactly 'control' or 'treatment' .
  • An arm can have zero users. It still contributes (0−N/2)2/(N/2)(0 - N/2)^2 / (N/2) to the statistic.
  • critical_values has exactly one row with df = 1 and alpha = 0.05 . It can also hold rows for other degrees of freedom and other significance levels.
  • Compare the unrounded statistic with the critical value. A statistic exactly equal to the critical value gives srm_flag = 0 .
  • Use the plain statistic above, with no continuity correction.