# The Rolling Peak

> The sweetest stretch in the sequence.

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

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given a list of numbers and positive integer k, return the maximum average of any contiguous subarray of length exactly k. If len(nums) < k, return None.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests the **sliding window technique** for computing running aggregates. It checks whether candidates can maintain a window sum incrementally instead of recomputing it from scratch each time.

---

### Break down the requirements

#### Step 1: Handle the edge case

If the list is shorter than k, return None.

#### Step 2: Compute the sum of the first window

Sum the first k elements as the initial window sum.

#### Step 3: Slide the window and track the maximum average

For each subsequent position, subtract the element leaving the window and add the element entering. Compare the new sum against the best.

#### Step 4: Return the maximum average

Divide the best sum by k.

---

### The solution

**Sliding window with incremental sum update**

```python
def max_avg_subarray(nums, k):
    if len(nums) < k:
        return None
    window_sum = sum(nums[:k])
    best_sum = window_sum
    for i in range(k, len(nums)):
        window_sum += nums[i] - nums[i - k]
        if window_sum > best_sum:
            best_sum = window_sum
    return best_sum / k
```

> **Time and Space Complexity**
>
> **Time:** O(n) with a single pass to slide the window.
> 
> **Space:** O(1). Only the window sum and best sum are tracked.

> **Interviewers Watch For**
>
> Do you maintain a running sum instead of recomputing? Recomputing the sum from scratch each window is O(n * k). The sliding approach is O(n).

> **Common Pitfall**
>
> Off-by-one in the sliding window bounds. The window starting at index i includes elements from i to i+k-1. The element leaving is `nums[i-k]`, not `nums[i-k-1]`.

---

## Common follow-up questions

- What if you needed the minimum average instead? _(Tests tracking the minimum sum alongside the maximum.)_
- What if k could change dynamically? _(Tests more complex window management with variable size.)_
- How would you find the maximum average of any subarray (variable length)? _(Tests binary search on the answer combined with prefix sums.)_
- What if the input were a stream? _(Tests maintaining a deque-based window with running statistics.)_

## Related

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