Outlier rule disagreement check: flag values by IQR, z-score and MAD rules in pandas

Quick Overview

Write a pandas function that flags each value of a numeric column with the IQR fence, the population z-score and the median absolute deviation rules, then returns the values at least one rule flags. It tests exact statistical constants, zero-spread handling and why outlier rules disagree on the same data.

Outlier rule disagreement check: flag values by IQR, z-score and MAD rules in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `method_disagreement(df, col)` that flags each non-null value of numeric column `col` by three outlier rules and returns rows flagged by AT LEAST ONE rule. All statistics (mean, median, std, quantiles) are computed over the non-null values only. The function must meet these requirements: - `by_iqr`: strictly outside `Q1 - 1.5*IQR` or `Q3 + 1.5*IQR` (quantiles with `interpolation='linear'`). - `by_zscore`: `abs((x - mean) / std) > 3.0` with population std `s.std(ddof=0)`. - `by_mad`: `abs(0.6745 * (x - median) / MAD) > 3.5`, `MAD = median(abs(x - median))` unscaled. If `MAD == 0`, nothing is flagged by this rule. - Columns exactly `['value', 'by_iqr', 'by_zscore', 'by_mad', 'n_methods']`: `by_*` are `bool`, `n_methods` (rules fired) is `int64`. - Preserve original index labels. Sort by `n_methods` descending, then `value` descending. ```hint One base series Drop the nulls once and keep that Series, index included. Every statistic and every flag comes from it, so the flags line up row by row. ``` ```hint Spread of zero Look at what each rule's division does when its spread estimate is zero, and compare that with what the spec says the rule should return. ``` ### Constraints & Assumptions - `pandas` and `numpy` are available. scipy is not required. ### What a Strong Answer Covers - Computing every statistic over the non-null values while keeping the original index labels on the output. - Each rule with its exact constants: strict IQR fences on linear quantiles, the population standard deviation for the z-score, and the unscaled MAD with 0.6745 and 3.5. - Handling a zero MAD (and a zero standard deviation) without producing infinite or NaN scores that flag the wrong rows. - The required columns and dtypes, the at-least-one-rule filter, and the two-key descending sort. - Why the three rules disagree on skewed or small samples. ### Follow-up Questions - Why does the `ddof` choice matter for the z-score rule? - Why is the MAD-based rule preferred on skewed data? - Can the z-score rule ever flag a value in a sample of 10 or fewer points? - If you had to pick one rule for an automated data-quality check, which would you choose and why?

Overview: Write a pandas function that flags each value of a numeric column with the IQR fence, the population z-score and the median absolute deviation rules, then returns the values at least one rule flags. It tests exact statistical constants, zero-spread handling and why outlier rules disagree on the same data.

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

Write method_disagreement(df, col) that flags each non-null value of numeric column col by three outlier rules and returns rows flagged by AT LEAST ONE rule. All statistics (mean, median, std, quantiles) are computed over the non-null values only.

The function must meet these requirements:

  • by_iqr : strictly outside Q1 - 1.5*IQR or Q3 + 1.5*IQR (quantiles with interpolation='linear' ).
  • by_zscore : abs((x - mean) / std) > 3.0 with population std s.std(ddof=0) .
  • by_mad : abs(0.6745 * (x - median) / MAD) > 3.5 , MAD = median(abs(x - median)) unscaled. If MAD == 0 , nothing is flagged by this rule.
  • Columns exactly ['value', 'by_iqr', 'by_zscore', 'by_mad', 'n_methods'] : by_* are bool , n_methods (rules fired) is int64 .
  • Preserve original index labels. Sort by n_methods descending, then value descending.

Constraints & Assumptions

  • pandas and numpy are available. scipy is not required.

What a Strong Answer Covers Guidance

  • Computing every statistic over the non-null values while keeping the original index labels on the output.
  • Each rule with its exact constants: strict IQR fences on linear quantiles, the population standard deviation for the z-score, and the unscaled MAD with 0.6745 and 3.5.
  • Handling a zero MAD (and a zero standard deviation) without producing infinite or NaN scores that flag the wrong rows.
  • The required columns and dtypes, the at-least-one-rule filter, and the two-key descending sort.
  • Why the three rules disagree on skewed or small samples.

Follow-up Questions Guidance

  • Why does the ddof choice matter for the z-score rule?
  • Why is the MAD-based rule preferred on skewed data?
  • Can the z-score rule ever flag a value in a sample of 10 or fewer points?
  • If you had to pick one rule for an automated data-quality check, which would you choose and why?