Segment Distribution Comparison Report: Median, Quartiles and IQR-to-Median Spread per Group in pandas

Quick Overview

Write a pandas function that summarizes a metric for each segment with its count, median, quartiles, IQR and IQR-to-median spread ratio, sorted by median. It tests grouped aggregation, quantile interpolation, deterministic tie-breaking and exact output dtypes.

Segment Distribution Comparison Report: Median, Quartiles and IQR-to-Median Spread per Group in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `segment_distribution_comparison(df, seg, metric)` that returns one row per value of the column `seg`, describing the median, the quartiles and the scale-free spread (`iqr / median`) of the column `metric`. The output must meet these rules: - Columns in this exact order: `segment` (object), `n` (int64), then `median`, `q1` (25th percentile), `q3` (75th percentile), `iqr` (`q3 - q1`) and `spread_ratio` (`iqr / median`), all `float64`. - Quartiles use pandas' default linear interpolation. - Sort by `median` descending, break ties by `segment` ascending, then call `reset_index(drop=True)`. ```hint One aggregation step `groupby(...).agg` accepts output names as keyword arguments, so the count, the median and both quartiles can come out of a single call with the right column names. ``` ```hint Equal medians Segments with the same typical value are exactly the ones this report is meant to tell apart. Check that your sort still gives a fixed row order for them. ``` ### Constraints & Assumptions - The result is graded with `assert_frame_equal` against a fixed frame, so column order, dtypes and the index all have to match. - `n` is the number of non-null `metric` values in the segment. ### What a Strong Answer Covers - Grouping by the segment column and computing the count, median and both quartiles with linear interpolation. - Deriving `iqr` and `spread_ratio`, and what the ratio shows that the IQR alone does not. - The exact column order and dtypes for an exact frame comparison. - A deterministic sort with the tie-breaker, followed by an index reset. - What happens when a median is zero, a segment label is missing, or a segment has no non-null metric values. ### Follow-up Questions - How would you guard against a division by zero when `median` is 0? - Would `spread_ratio` alone confirm that a spread difference between segments is real? - When does `iqr / median` mislead, for example on a metric that can be negative? - How would you produce the same report in PostgreSQL?

Overview: Write a pandas function that summarizes a metric for each segment with its count, median, quartiles, IQR and IQR-to-median spread ratio, sorted by median. It tests grouped aggregation, quantile interpolation, deterministic tie-breaking and exact output dtypes.

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

Write segment_distribution_comparison(df, seg, metric) that returns one row per value of the column seg, describing the median, the quartiles and the scale-free spread (iqr / median) of the column metric.

The output must meet these rules:

  • Columns in this exact order: segment (object), n (int64), then median , q1 (25th percentile), q3 (75th percentile), iqr ( q3 - q1 ) and spread_ratio ( iqr / median ), all float64 .
  • Quartiles use pandas' default linear interpolation.
  • Sort by median descending, break ties by segment ascending, then call reset_index(drop=True) .

Constraints & Assumptions

  • The result is graded with assert_frame_equal against a fixed frame, so column order, dtypes and the index all have to match.
  • n is the number of non-null metric values in the segment.

What a Strong Answer Covers Guidance

  • Grouping by the segment column and computing the count, median and both quartiles with linear interpolation.
  • Deriving iqr and spread_ratio , and what the ratio shows that the IQR alone does not.
  • The exact column order and dtypes for an exact frame comparison.
  • A deterministic sort with the tie-breaker, followed by an index reset.
  • What happens when a median is zero, a segment label is missing, or a segment has no non-null metric values.

Follow-up Questions Guidance

  • How would you guard against a division by zero when median is 0?
  • Would spread_ratio alone confirm that a spread difference between segments is real?
  • When does iqr / median mislead, for example on a metric that can be negative?
  • How would you produce the same report in PostgreSQL?