# Column Max

> One value rules the column.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a non-empty list of numbers, return the maximum.

## Worked solution and explanation

### Why this problem exists in real interviews

Finding the maximum value in a list is the most basic aggregation operation. It tests whether you can write a correct loop with a running accumulator and handle the initialization correctly.

---

### Break down the requirements

#### Step 1: Initialize with the first element

Set the running max to the first element. This avoids issues with initializing to 0 (which fails for all-negative lists) or negative infinity.

#### Step 2: Scan and update

Compare each subsequent element to the running max and update if larger.

---

### The solution

**Single-pass scan with running maximum**

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

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the list length. Every element is compared exactly once.
> 
> **Space:** O(1). Only a single variable tracks the running maximum.

> **Interviewers Watch For**
>
> Initializing `current_max` to `nums[0]` rather than `0` or `-float('inf')`. Using the first element is the safest approach when the list is guaranteed non-empty.

> **Common Pitfall**
>
> Initializing the max to 0. This produces the wrong answer for lists like `[-5, -3, -1]` where all values are negative.

---

## Common follow-up questions

- What if the list could be empty? _(Tests whether you add a guard clause and define the return value for empty input.)_
- How would you find both the max value and its index? _(Tests tracking a second variable alongside the running max.)_
- What if you needed the top-k values instead of just the max? _(Tests knowledge of heapq.nlargest or partial sorting strategies.)_

## Related

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