Sample ratio mismatch check: compare each A/B variant's user share to 0.5 in SQL

Quick Overview

Write a SQL query for a sample ratio mismatch check that counts users per A/B variant, computes each arm's observed share of the total, and compares it with the expected 0.5. It tests grouped counts against a table-wide total, decimal division, and careful rounding.

Sample ratio mismatch check: compare each A/B variant's user share to 0.5 in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `assignments` table has one row per user with the `variant` (`'A'` or `'B'`) that user was assigned to. For a sample-ratio-mismatch check, compare each variant's observed share of users against the expected 0.5. Return one row per `variant` with: - `users`: the row count for that variant - `observed_share`: `users` divided by the total number of users, rounded to 4 decimals - `expected_share`: fixed at 0.5 - `share_diff`: `observed_share` minus `expected_share`, rounded to 4 decimals ```hint Division Check what your engine returns when it divides one integer count by another. ``` ### Input Table(s) Table `assignments` | Column | Type | Meaning | |---|---|---| | variant | VARCHAR(5) | The arm the user was assigned to, `'A'` or `'B'` | Each row is one user. The table has no other columns and no primary key. ### Output Contract - Columns, in this order: `variant`, `users`, `observed_share`, `expected_share`, `share_diff`. - One row per variant, sorted by `variant` ascending. - `observed_share` and `share_diff` are rounded to 4 decimal places. `expected_share` is the constant 0.5. ### Example Input `assignments` (21 rows, listed top to bottom): | variant | |---| | A | | A | | A | | A | | B | | A | | B | | B | | A | | A | | B | | B | | A | | B | | A | | A | | B | | A | | B | | B | | B | Expected output: | variant | users | observed_share | expected_share | share_diff | |---|---|---|---|---| | A | 11 | 0.5238 | 0.5 | 0.0238 | | B | 10 | 0.4762 | 0.5 | -0.0238 | There are 11 A rows and 10 B rows out of 21, so the shares are 11/21 = 0.523809... and 10/21 = 0.476190..., each about 0.0238 away from 0.5. ### Constraints and Clarifications - `variant` is never NULL and is always `'A'` or `'B'`. - Both variants appear at least once. - Each row is a distinct user, so every row counts once. - Compute `share_diff` from the unrounded share (count divided by total, minus 0.5) and round the result to 4 decimals.

Overview: Write a SQL query for a sample ratio mismatch check that counts users per A/B variant, computes each arm's observed share of the total, and compares it with the expected 0.5. It tests grouped counts against a table-wide total, decimal division, and careful rounding.

PracHub logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The assignments table has one row per user with the variant ('A' or 'B') that user was assigned to. For a sample-ratio-mismatch check, compare each variant's observed share of users against the expected 0.5. Return one row per variant with:

  • users : the row count for that variant
  • observed_share : users divided by the total number of users, rounded to 4 decimals
  • expected_share : fixed at 0.5
  • share_diff : observed_share minus expected_share , rounded to 4 decimals

Input Table(s)

Table assignments

ColumnTypeMeaning
variantVARCHAR(5)The arm the user was assigned to, 'A' or 'B'

Each row is one user. The table has no other columns and no primary key.

Output Contract

  • Columns, in this order: variant , users , observed_share , expected_share , share_diff .
  • One row per variant, sorted by variant ascending.
  • observed_share and share_diff are rounded to 4 decimal places. expected_share is the constant 0.5.

Example

Input assignments (21 rows, listed top to bottom):

variant
A
A
A
A
B
A
B
B
A
A
B
B
A
B
A
A
B
A
B
B
B

Expected output:

variantusersobserved_shareexpected_shareshare_diff
A110.52380.50.0238
B100.47620.5-0.0238

There are 11 A rows and 10 B rows out of 21, so the shares are 11/21 = 0.523809... and 10/21 = 0.476190..., each about 0.0238 away from 0.5.

Constraints and Clarifications

  • variant is never NULL and is always 'A' or 'B' .
  • Both variants appear at least once.
  • Each row is a distinct user, so every row counts once.
  • Compute share_diff from the unrounded share (count divided by total, minus 0.5) and round the result to 4 decimals.