Arithmetic Mean & Median of a Sample

Basics
BasicsCentral TendencyNumPyGoogleMetaJane Street

Problem

Implement mean_and_median(x) returning (mean, median) as Python floats for a non-empty 1D numeric sequence x.

Why It Matters: Comparing mean vs. median instantly reveals skewed distributions and outliers—for example, median salary or latency (P50) is far more robust than the arithmetic mean.

Function Signature

import numpy as np

def mean_and_median(x) -> tuple[float, float]:

Sample Input & Output

Example 1: Skewed Sample With an Outlier

Input:

x = [10, 12, 14, 16, 100]

Expected Output:

(30.4, 14.0)

Explanation: The outlier 100 pulls the mean up to 30.4, while the median stays robust at 14.0.

Example 2: Even-Length Sample

Input:

x = [4, 1, 9, 2]

Expected Output:

(4.0, 3.0)

Explanation: Sorted list is [1, 2, 4, 9]; mean is 16 / 4 = 4.0, and median is (2 + 4) / 2 = 3.0.

Constraints & Edge Cases

  • Return (mean, median) as Python floats.
  • Return a 2-tuple of Python floats (float, float).
  • For even-length inputs, the median is the average of the two middle sorted values.

Constraints & Evaluation Criteria

  • Return `(mean, median)` as Python floats.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[1, 2, 3, 4, 100]
Expected Output / Criteria
(22.0, 3.0)