Per-Day SRM Alarm Detection with a Chi-Square Test
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `def daily_srm_alarms(daily: pd.DataFrame) -> list[int]` in `solution.py`. `daily` has one row per day with integer columns `'day'`, `'control'` and `'treatment'` (per-day counts, not cumulative). Run a 50/50 chi-square sample ratio mismatch (SRM) check per day and return the flagged days.
The function must follow these rules:
- Per row, compute `t = control + treatment` and call `scipy.stats.chisquare(f_obs=[control, treatment], f_exp=[t/2, t/2])`.
- Flag the day when its p-value is `< 0.001`. The comparison is strict, so a p-value of exactly `0.001` is not an alarm.
- Return the `'day'` values (not row indices) as plain Python ints, sorted ascending.
`pandas` and `scipy` are available. Cumulative aggregation is out of scope.
Example:
```python
# days 1..5
control = [5000, 5010, 4980, 5200, 5030]
treatment = [5005, 4990, 5020, 4600, 4970]
daily_srm_alarms(daily) -> [4]
# day 4's 5200/4600 split gives p ~ 1.35e-09 < 0.001
```
```hint Check the output rules
Reread what the function returns and when a day counts as an alarm. Day labels and row positions can differ, numpy hands you its own integer type, and the threshold comparison is strict.
```
### Constraints & Assumptions
- `daily` has one row per day, and `control` and `treatment` are that day's counts alone, never running totals.
- The design split is 50/50, so each arm's expected count is half the day's total.
- A day is an alarm only when its p-value is strictly below 0.001.
- The output is ascending by `'day'` whatever the row order of `daily`.
### What a Strong Answer Covers
- A goodness-of-fit test on each day's two counts in isolation, with expected counts taken from the 50/50 design.
- The pinned output details: `'day'` labels in place of the index, the strict `<`, plain Python ints, ascending order.
- Time and space complexity over n days.
- Edge cases such as an empty frame or a day with no traffic in either arm.
### Follow-up Questions
- How would you adapt this to an uneven split, such as 90/10?
- With many days, doesn't testing each day inflate false positives? How would you control that?
- An alarm fires on a single day and the other days look clean. What would you check before deciding the experiment is broken?
Overview: Write a Python function that runs a 50/50 chi-square sample ratio mismatch check on each day of an A/B test and returns the days that trip the alarm. It tests careful use of pandas and scipy, exact output rules, and awareness of false alarms when every day is tested.