Median Calculation

PySpark data engineering interview problem. Difficulty: intermediate. Pattern: Aggregation. About 16 minutes. Part of the Pro drill bank.

Median amount for an odd-sized fixture (not the mean). Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

The median is the middle value when the numbers are sorted. It is often better than the average, because one very large value does not change it much. For 1, 2, 3, 4 and 100, the average is 22 but the median is 3. Return 1 row with a column median_amount that holds the median of amount in df. Do not use percentile_approx, which gives an approximate answer. Work it out by sorting the values and picking the middle one. The editor creates the table df and ends with result = df. Change the last line to build your answer.

Requirements

  • Return 1 row with 1 column named median_amount.
  • Calculate the real median by sorting, not an approximation.
  • Do not use percentile_approx.
  • Store the DataFrame in result.

Constraints

  • The table has 5 values: 1, 2, 3, 4 and 100.
  • With an odd number of values, the median is the single middle value after sorting.
  • With an even number of values, the median is the average of the two middle values.

Examples

Input: amounts 1,2,3,4,100 Output: median_amount 3.0 Odd count: middle of sorted values.

Topics: lakebench, pyspark, median, orderBy.

More PySpark interview questions · All interview problems · Learn data engineering

intermediate

Median Calculation

Interview-style drill: Median amount for an odd-sized fixture (not the mean).

Return 1 row with `median_amount` = median of `amount`. The fixture sorts to 1,2,3,4,100 so the median is 3 (mean would be 22). Assign `result`. Do not use percentile_approx.