# The Halftime Score

> Middle value of a dataset. No built-in shortcuts.

Canonical URL: <https://datadriven.io/problems/the_halftime_score>

Domain: Python · Difficulty: easy · Seniority: L4

## Problem

Given a non-empty list of numbers, return its median. For even length, return the average of the two middle values. Do not rely on external libraries.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **median computation without library functions**, probing whether a candidate understands the definition of median and can handle both odd and even list lengths.

---

### Break down the requirements

#### Step 1: Sort the list

The median requires ordered data.

#### Step 2: Handle odd and even lengths

Odd: return the middle element. Even: return the average of the two middle elements.

---

### The solution

**Sort and select median**

```python
def compute_median(nums: list) -> float:
    sorted_nums = sorted(nums)
    n = len(sorted_nums)
    if n % 2 == 1:
        median = float(sorted_nums[n // 2])
    else:
        left = sorted_nums[n // 2 - 1]
        right = sorted_nums[n // 2]
        median = (left + right) / 2.0
    return median
```

> **Time and Space Complexity**
>
> **Time:** O(n log n) for sorting.
> 
> **Space:** O(n) for the sorted copy.

> **Interviewers Watch For**
>
> Whether you handle the even-length case correctly. The two center elements are at indices `n//2 - 1` and `n//2`, and their average must be a float.

> **Common Pitfall**
>
> Using integer division for the average. `(a + b) // 2` truncates; use `(a + b) / 2.0` for the correct float result.

---

## Common follow-up questions

- How would you find the median without sorting? _(Tests quickselect algorithm for O(n) average-case median.)_
- What if the data is streaming? _(Tests the two-heap approach: max-heap for the lower half, min-heap for the upper half.)_
- What is the difference between median and mean for skewed data? _(Tests statistical reasoning about robustness to outliers.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_halftime_score)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.