Maintain a Moving Average with a Circular Buffer

Quick Overview

Maintain one moving average per incoming value using the values seen so far until the window fills, then exactly the latest `k` values in a circular-buffer design.

Maintain a Moving Average with a Circular Buffer

Company: Coinbase

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem Maintain the moving average of the most recent `k` values in a stream. For each new value, return the average of all values seen so far when fewer than `k` values exist; afterward return the average of exactly the latest `k` values. ### Function Contract Implement `movingAverages(k, values)` and return one floating-point average for each input value. ### Constraints & Assumptions - `1 <= k <= 200,000`. - `0 <= len(values) <= 200,000`. - Values are integers in `[-10^9, 10^9]`. - Use a numeric accumulator wide enough for `k * value`. - Each insertion should take `O(1)` time. ### Clarifying Questions to Ask - What denominator is used before the window fills? The number of values received so far. - Does the oldest value leave before or after the new value is included? Replace the oldest, then average the current window. - Is thread safety required? Not for the core function; discuss it as a follow-up. ```hint Reuse a circular slot At step `i`, the overwritten position is `i mod k`. Subtract its old value only after the first `k` insertions, then store and add the new value. ``` ### Example ```text k = 3, values = [1,10,3,5] result = [1.0, 5.5, 4.6666666667, 6.0] ``` ### Evaluation Focus - Uses the correct denominator during warm-up. - Removes exactly the value leaving the circular window. - Handles `k = 1`, negative numbers, and empty input. - Runs in `O(n)` total time and `O(k)` space. ### Extensions to Discuss 1. How would repeated floating-point additions and subtractions accumulate error? 2. How could periodic recomputation bound drift, and what is its amortized cost? 3. What synchronization would make a stateful `next` method linearizable?

Quick Answer: Maintain one moving average per incoming value using the values seen so far until the window fills, then exactly the latest `k` values in a circular-buffer design.

|Home/Coding & Algorithms/Coinbase
Coinbase logo
Coinbase
May 12, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
1
0

Problem

Maintain the moving average of the most recent k values in a stream. For each new value, return the average of all values seen so far when fewer than k values exist; afterward return the average of exactly the latest k values.

Function Contract

Implement movingAverages(k, values) and return one floating-point average for each input value.

Constraints & Assumptions

  • 1 <= k <= 200,000 .
  • 0 <= len(values) <= 200,000 .
  • Values are integers in [-10^9, 10^9] .
  • Use a numeric accumulator wide enough for k * value .
  • Each insertion should take O(1) time.

Clarifying Questions to Ask Guidance

  • What denominator is used before the window fills? The number of values received so far.
  • Does the oldest value leave before or after the new value is included? Replace the oldest, then average the current window.
  • Is thread safety required? Not for the core function; discuss it as a follow-up.

Example

k = 3, values = [1,10,3,5]
result = [1.0, 5.5, 4.6666666667, 6.0]

Evaluation Focus

  • Uses the correct denominator during warm-up.
  • Removes exactly the value leaving the circular window.
  • Handles k = 1 , negative numbers, and empty input.
  • Runs in O(n) total time and O(k) space.

Extensions to Discuss

  1. How would repeated floating-point additions and subtractions accumulate error?
  2. How could periodic recomputation bound drift, and what is its amortized cost?
  3. What synchronization would make a stateful next method linearizable?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...