Count IQR Outliers With Tukey Fences
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `count_iqr_outliers(df: pd.DataFrame, col: str) -> int` that returns how many rows have a value in `df[col]` outside the 1.5×IQR Tukey fences.
Requirements:
- Compute `Q1`/`Q3` with `quantile(0.25)`/`quantile(0.75)` using `interpolation='linear'`, and `IQR = Q3 - Q1`.
- Count values where `value < Q1 - 1.5*IQR` or `value > Q3 + 1.5*IQR` (strict).
- Return a plain Python `int` via `int(mask.sum())`, not a NumPy type, because the result is graded as an exact scalar.
Example:
```python
df = pd.DataFrame({"x": [10, 12, 11, 13, 12, 200]})
count_iqr_outliers(df, "x") # -> 1 (200 exceeds Q3 + 1.5*IQR)
```
```hint Work the example by hand
With six values, linear interpolation puts Q1 a quarter of the way between the second and third sorted values, so the fences need not be whole numbers. Compute both fences for the example, then decide what your comparisons do with a value that sits exactly on a fence.
```
### Constraints & Assumptions
- `col` is numeric. NaN handling and dtype coercion are out of scope.
### What a Strong Answer Covers
- Computes Q1 and Q3 with linear interpolation and derives both fences, checked against the example.
- Uses strict comparisons on both sides, so values exactly on a fence are not counted.
- Returns a native Python `int` and explains why the raw sum is a NumPy type.
- Describes what happens with a zero IQR or an empty column.
### Follow-up Questions
- How would you count outliers per group, with fences computed inside each group?
- How does the count change with 3×IQR fences, and when would you use them?
- Why can the 1.5×IQR rule flag many points on a right-skewed column such as revenue, and what would you do about it?
- If the column could contain NaN, how would your function treat those rows?
Overview: Write a pandas function that counts how many values in a numeric column fall strictly outside the Tukey 1.5×IQR fences and returns a plain Python int. It tests quartile computation with linear interpolation, strict boundary comparisons and counting with a boolean mask.