Compute Per-Cell Chi-Square Contributions for a Segment-by-Outcome Table in SQL
Quick Overview
Write a PostgreSQL query that returns each cell's chi-square contribution in a segment-by-conversion contingency table. It tests computing marginals and expected counts in SQL, keeping cells with zero observations, avoiding integer division and returning the rows in a fully specified order.
Compute Per-Cell Chi-Square Contributions for a Segment-by-Outcome Table in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `segment_outcomes` table has one row per customer, with the customer's `segment` (`'starter'`, `'pro'` or `'enterprise'`) and `outcome` (`'converted'` or `'not_converted'`). Build the segment-by-outcome contingency table and compute each cell's chi-square contribution:
$$\text{contribution}_{ij} = \frac{(O_{ij} - E_{ij})^2}{E_{ij}}, \qquad E_{ij} = \frac{R_i \cdot C_j}{N}$$
Here $R_i$ is the total for segment $i$, $C_j$ is the total for outcome $j$ and $N$ is the grand total. Return one row per cell with `segment`, `outcome` and `contribution` rounded to 4 decimals.
```hint Where the cells come from
If the list of cells comes from grouping the observed rows, a segment-outcome pair with zero customers never shows up. Decide where the full list of cells should come from.
```
### Input Table(s)
`segment_outcomes`
| Column | Type | Meaning |
|---|---|---|
| segment | VARCHAR(20) | Pricing segment: `'starter'`, `'pro'` or `'enterprise'` |
| outcome | VARCHAR(20) | `'converted'` or `'not_converted'` |
The table has no key column. Each row is one customer, so many rows share the same (`segment`, `outcome`) values.
### Output Contract
| Column | Type | Meaning |
|---|---|---|
| segment | VARCHAR | Segment of the cell |
| outcome | VARCHAR | Outcome of the cell |
| contribution | NUMERIC | $(O - E)^2 / E$ for the cell, rounded to 4 decimal places |
Return one row for every combination of a segment present in the table and an outcome present in the table. Order by `outcome` ascending, then `contribution` (the rounded value) descending, then `segment` ascending. (`segment`, `outcome`) is unique, so the order is fully determined.
### Example
Input `segment_outcomes`:
| segment | outcome |
|---|---|
| starter | converted |
| enterprise | converted |
| starter | not_converted |
| pro | converted |
| starter | not_converted |
| pro | not_converted |
| enterprise | converted |
| pro | not_converted |
| enterprise | converted |
| pro | not_converted |
| starter | converted |
| starter | not_converted |
| pro | not_converted |
| pro | converted |
| enterprise | not_converted |
| pro | not_converted |
| enterprise | not_converted |
| enterprise | converted |
| starter | converted |
| starter | converted |
Output:
| segment | outcome | contribution |
|---|---|---|
| pro | converted | 0.6429 |
| enterprise | converted | 0.3333 |
| starter | converted | 0.0714 |
| pro | not_converted | 0.6429 |
| enterprise | not_converted | 0.3333 |
| starter | not_converted | 0.0714 |
Starter has 4 conversions out of 7, enterprise 4 out of 6 and pro 2 out of 7, so 10 of the 20 customers converted. Pro's cells are furthest from their expected count of 3.5, which gives them the largest contributions.
### Constraints and Clarifications
- Neither column is NULL.
- Do not hard-code the segment labels. Only segments that appear in the table get rows; a segment with no customers is left out.
- Both outcome values appear in the table, so every expected count is positive.
- A segment can have zero customers with one of the outcomes. That cell still gets a row, computed with an observed count of 0.
- Each row is a different customer. Count rows as they are; there is nothing to deduplicate.
- Round each cell's contribution on its own.
Overview: Write a PostgreSQL query that returns each cell's chi-square contribution in a segment-by-conversion contingency table. It tests computing marginals and expected counts in SQL, keeping cells with zero observations, avoiding integer division and returning the rows in a fully specified order.
The segment_outcomes table has one row per customer, with the customer's segment ('starter', 'pro' or 'enterprise') and outcome ('converted' or 'not_converted'). Build the segment-by-outcome contingency table and compute each cell's chi-square contribution:
contributionij=Eij(Oij−Eij)2,Eij=NRi⋅Cj
Here Ri is the total for segment i, Cj is the total for outcome j and N is the grand total. Return one row per cell with segment, outcome and contribution rounded to 4 decimals.
Input Table(s)
segment_outcomes
Column
Type
Meaning
segment
VARCHAR(20)
Pricing segment: 'starter', 'pro' or 'enterprise'
outcome
VARCHAR(20)
'converted' or 'not_converted'
The table has no key column. Each row is one customer, so many rows share the same (segment, outcome) values.
Output Contract
Column
Type
Meaning
segment
VARCHAR
Segment of the cell
outcome
VARCHAR
Outcome of the cell
contribution
NUMERIC
(O−E)2/E for the cell, rounded to 4 decimal places
Return one row for every combination of a segment present in the table and an outcome present in the table. Order by outcome ascending, then contribution (the rounded value) descending, then segment ascending. (segment, outcome) is unique, so the order is fully determined.
Example
Input segment_outcomes:
segment
outcome
starter
converted
enterprise
converted
starter
not_converted
pro
converted
starter
not_converted
pro
not_converted
enterprise
converted
pro
not_converted
enterprise
converted
pro
not_converted
starter
converted
starter
not_converted
pro
not_converted
pro
converted
enterprise
not_converted
pro
not_converted
enterprise
not_converted
enterprise
converted
starter
converted
starter
converted
Output:
segment
outcome
contribution
pro
converted
0.6429
enterprise
converted
0.3333
starter
converted
0.0714
pro
not_converted
0.6429
enterprise
not_converted
0.3333
starter
not_converted
0.0714
Starter has 4 conversions out of 7, enterprise 4 out of 6 and pro 2 out of 7, so 10 of the 20 customers converted. Pro's cells are furthest from their expected count of 3.5, which gives them the largest contributions.
Constraints and Clarifications
Neither column is NULL.
Do not hard-code the segment labels. Only segments that appear in the table get rows; a segment with no customers is left out.
Both outcome values appear in the table, so every expected count is positive.
A segment can have zero customers with one of the outcomes. That cell still gets a row, computed with an observed count of 0.
Each row is a different customer. Count rows as they are; there is nothing to deduplicate.