Compute a Moving Average from a Data Stream

Quick Overview

Compute moving averages from a stream with a bounded queue and running sum, handling warm-up, oldest-value eviction, and numeric precision correctly.

Compute a Moving Average from a Data Stream

Company: Squarepoint

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement a moving-average object with a positive window capacity w. Each call `next(value)` adds a number and returns the average of the most recent w values, or of all values seen so far if fewer than w have arrived. ### Constraints & Assumptions This is the standard moving-average stream formulation of the named problem. The window is defined by item count, not elapsed time. ### Clarifying Questions to Ask - During warm-up, is the divisor the number of values received or the full capacity? - Should numeric precision use floating-point values or exact arithmetic? ```hint Keep a removable contribution When the window is full, the oldest value must leave the running aggregate before the new average is calculated. ``` ### What a Strong Answer Covers - A bounded queue or circular buffer and running sum. - Correct warm-up divisor and eviction order. - Constant-time updates and numeric precision considerations. ### Follow-up Questions - How would the implementation change for a time-based window? - How could long-running floating-point accumulation drift be reduced?

Overview: Compute moving averages from a stream with a bounded queue and running sum, handling warm-up, oldest-value eviction, and numeric precision correctly.

Squarepoint logo
Squarepoint
Mar 15, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Implement a moving-average object with a positive window capacity w. Each call next(value) adds a number and returns the average of the most recent w values, or of all values seen so far if fewer than w have arrived.

Constraints & Assumptions

This is the standard moving-average stream formulation of the named problem. The window is defined by item count, not elapsed time.

Clarifying Questions to Ask Guidance

  • During warm-up, is the divisor the number of values received or the full capacity?
  • Should numeric precision use floating-point values or exact arithmetic?

What a Strong Answer Covers Guidance

  • A bounded queue or circular buffer and running sum.
  • Correct warm-up divisor and eviction order.
  • Constant-time updates and numeric precision considerations.

Follow-up Questions Guidance

  • How would the implementation change for a time-based window?
  • How could long-running floating-point accumulation drift be reduced?