# Column Range

> From minimum to maximum. What is the spread?

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a non-empty list of numbers, return a 2-element list [min_value, max_value].

## Worked solution and explanation

### Why this problem exists in real interviews

Computing the min and max of a column in a single pass tests whether you can **combine two aggregations** efficiently. It is a basic but revealing check on loop structure and variable initialization.

---

### Break down the requirements

#### Step 1: Initialize both min and max from the first element

Using the first element avoids sentinel value problems with 0 or infinity.

#### Step 2: Update both in a single pass

For each element, compare against both the running min and running max. This avoids scanning the list twice.

---

### The solution

**Single-pass min and max tracking**

```python
def min_and_max(nums):
    current_min = nums[0]
    current_max = nums[0]
    for i in range(1, len(nums)):
        if nums[i] < current_min:
            current_min = nums[i]
        if nums[i] > current_max:
            current_max = nums[i]
    return [current_min, current_max]
```

> **Time and Space Complexity**
>
> **Time:** O(n) with a single pass. Two comparisons per element.
> 
> **Space:** O(1). Two tracking variables.

> **Interviewers Watch For**
>
> Doing both aggregations in one loop instead of calling `min()` then `max()` separately. Two built-in calls scan the list twice; a single loop is more efficient.

> **Common Pitfall**
>
> Returning `(min, max)` as a tuple when the problem specifies a list `[min, max]`. Always match the expected return type.

---

## Common follow-up questions

- What if you also needed the median? _(Tests awareness that median requires sorting, making a single-pass approach insufficient.)_
- How would you handle None values in the list? _(Tests defensive coding: skip Nones or raise on unexpected nulls.)_
- Can you find min and max with fewer than 2n comparisons? _(Tests knowledge of the tournament method: compare pairs first, then update min/max from the pair, reducing total comparisons to roughly 1.5n.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/column_range)
- [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.