Moving Average from Data Stream

Easy · Queue & Deque · about 10 minutes

Asked at Meta, Google, Spotify, Amazon, Twitch.

Implement MovingAverage, which averages the most recent values of a stream over a fixed-size window.

  • MovingAverage(size) sets the window size.
  • next(val) takes in val and returns the mean of the latest size values seen; while fewer than size

values have arrived, it averages all of them.

Related guides