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
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 resultCommon 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.)