Run a chi-square goodness-of-fit test on die rolls against a weighted distribution in SQL

Quick Overview

Write a SQL query that computes a chi-square goodness-of-fit statistic for die rolls against a weighted expected distribution and labels the die fair or loaded. It tests a LEFT JOIN that keeps faces nobody rolled, turning weights into expected counts without integer division, and using a critical value.

Run a chi-square goodness-of-fit test on die rolls against a weighted distribution in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

`rolls` has one row per throw of a six-sided die (`face` 1-6). `expected_dist` maps each `face` to a `weight`, and the expected proportion for a face is its weight divided by the sum of all weights. Return one row with the chi-square goodness-of-fit statistic and a verdict on whether the die is fair. - `chi_square` = SUM((observed - expected)^2 / expected) over the six faces, where expected = total rolls * proportion. Round it to 4 decimals. - A face with zero observed rolls still contributes to the sum, so the counts must be joined from `expected_dist` with a `LEFT JOIN`. - `verdict` is `'loaded'` when `chi_square` exceeds 11.07 (the critical value for 5 degrees of freedom), otherwise `'fair'`. ```hint Single-row totals The total number of rolls and the sum of the weights are single values that every face needs. Compute them once and attach them to each face. ``` ### Input Table(s) Table `rolls`: | Column | Type | Meaning | |---|---|---| | face | INT | The face shown on one throw, 1 to 6 | No key. Each row is one throw, so the same face appears many times. Table `expected_dist`: | Column | Type | Meaning | |---|---|---| | face | INT | A die face, 1 to 6 (primary key) | | weight | INT | Relative weight of that face | ### Output Contract - Exactly one row with columns `chi_square` and `verdict`, in that order. - `chi_square` is the statistic summed over all six faces in `expected_dist`, rounded to 4 decimals. - `verdict` is `'loaded'` when the unrounded statistic is strictly greater than 11.07, otherwise `'fair'`. ### Example Input: ```text rolls +------+ | face | +------+ | 4 | | 2 | | 5 | | 4 | | 4 | | 4 | | 1 | | 2 | | 3 | | 2 | | 3 | | 6 | | 5 | | 5 | | 2 | | 3 | | 2 | | 6 | | 5 | | 1 | | 4 | +------+ expected_dist +------+--------+ | face | weight | +------+--------+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 1 | | 5 | 1 | | 6 | 1 | +------+--------+ ``` Expected output: ```text +------------+---------+ | chi_square | verdict | +------------+---------+ | 2.7143 | fair | +------------+---------+ ``` There are 21 rolls and equal weights, so each face expects 3.5. The observed counts for faces 1 to 6 are 2, 5, 3, 5, 4 and 2. The squared gaps add up to 9.5, and 9.5 / 3.5 = 2.7143, which is below 11.07. ### Constraints and Clarifications - `rolls` has at least one row, and every `face` in it is between 1 and 6. Neither table has NULLs. - `expected_dist` has exactly six rows, one per face 1 to 6, and every weight is a positive integer. The weights do not have to sum to anything in particular. - A face that never appears in `rolls` has an observed count of 0.

Overview: Write a SQL query that computes a chi-square goodness-of-fit statistic for die rolls against a weighted expected distribution and labels the die fair or loaded. It tests a LEFT JOIN that keeps faces nobody rolled, turning weights into expected counts without integer division, and using a critical value.

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

rolls has one row per throw of a six-sided die (face 1-6). expected_dist maps each face to a weight, and the expected proportion for a face is its weight divided by the sum of all weights. Return one row with the chi-square goodness-of-fit statistic and a verdict on whether the die is fair.

  • chi_square = SUM((observed - expected)^2 / expected) over the six faces, where expected = total rolls * proportion. Round it to 4 decimals.
  • A face with zero observed rolls still contributes to the sum, so the counts must be joined from expected_dist with a LEFT JOIN .
  • verdict is 'loaded' when chi_square exceeds 11.07 (the critical value for 5 degrees of freedom), otherwise 'fair' .

Input Table(s)

Table rolls:

ColumnTypeMeaning
faceINTThe face shown on one throw, 1 to 6

No key. Each row is one throw, so the same face appears many times.

Table expected_dist:

ColumnTypeMeaning
faceINTA die face, 1 to 6 (primary key)
weightINTRelative weight of that face

Output Contract

  • Exactly one row with columns chi_square and verdict , in that order.
  • chi_square is the statistic summed over all six faces in expected_dist , rounded to 4 decimals.
  • verdict is 'loaded' when the unrounded statistic is strictly greater than 11.07, otherwise 'fair' .

Example

Input:

rolls
+------+
| face |
+------+
| 4    |
| 2    |
| 5    |
| 4    |
| 4    |
| 4    |
| 1    |
| 2    |
| 3    |
| 2    |
| 3    |
| 6    |
| 5    |
| 5    |
| 2    |
| 3    |
| 2    |
| 6    |
| 5    |
| 1    |
| 4    |
+------+

expected_dist
+------+--------+
| face | weight |
+------+--------+
| 1    | 1      |
| 2    | 1      |
| 3    | 1      |
| 4    | 1      |
| 5    | 1      |
| 6    | 1      |
+------+--------+

Expected output:

+------------+---------+
| chi_square | verdict |
+------------+---------+
| 2.7143     | fair    |
+------------+---------+

There are 21 rolls and equal weights, so each face expects 3.5. The observed counts for faces 1 to 6 are 2, 5, 3, 5, 4 and 2. The squared gaps add up to 9.5, and 9.5 / 3.5 = 2.7143, which is below 11.07.

Constraints and Clarifications

  • rolls has at least one row, and every face in it is between 1 and 6. Neither table has NULLs.
  • expected_dist has exactly six rows, one per face 1 to 6, and every weight is a positive integer. The weights do not have to sum to anything in particular.
  • A face that never appears in rolls has an observed count of 0.