The Bug Spotter

An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
easy
Seniority
L3
Asked in
Semrush

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.

It compiles. The answer is still wrong.

Example

Input:

nums = [10,20,30]

Output:

[10,15,20]

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
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

Common follow-up questions