# The Bug Spotter

> It compiles. The answer is still wrong.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list of numbers, return a list of the same length where position i is the average of nums[0..i] (inclusive). Use float division.

## Worked solution and explanation

### Why this problem exists in real interviews

Debugging a provided implementation tests **code reading**, **tracing execution**, and **identifying logical errors**. It reveals whether you can systematically find bugs by walking through examples.

---

### Break down the requirements

#### Step 1: Compute the running average at each position

At index i, the running average is the sum of elements 0 through i, divided by (i + 1).

#### Step 2: Return a list of running averages

Each position gets the mean of all values seen so far.

---

### The solution

**Correct running average with proper division and accumulation**

```python
def fix_running_average(nums):
    result = []
    total = 0
    for i in range(len(nums)):
        total += nums[i]
        avg = total / (i + 1)
        result.append(avg)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the list length.
> 
> **Space:** O(n) for the result list.

> **Interviewers Watch For**
>
> Identifying both bugs systematically. Common bugs in running-average implementations: dividing by `i` instead of `i + 1` (off-by-one), and resetting the total inside the loop instead of accumulating.

> **Common Pitfall**
>
> Dividing by `i` instead of `i + 1`. At index 0, `i` is 0, causing a ZeroDivisionError. At index 1, dividing by 1 gives the second element instead of the average of the first two.

---

## Common follow-up questions

- How would you debug this without knowing what the bugs are? _(Tests systematic debugging: run through a small example step by step and compare expected vs. actual at each position.)_
- What if the function should return integers instead of floats? _(Tests using integer division `//` and understanding when truncation vs. rounding matters.)_
- How would you compute a running median instead? _(Tests maintaining a sorted structure (two heaps) for O(log n) per insertion.)_

## Related

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