Quick Overview

This question evaluates understanding of streaming algorithms and sliding-window aggregation within the Coding & Algorithms domain, focusing on incremental computation, state management, and numerical stability when computing moving averages.

Compute moving average over last N stream

Company: Atlassian

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given an integer N and an unbounded stream of integers arriving one by one. After each new integer arrives, output the average of the last N integers seen so far. Requirements: - If fewer than N integers have arrived, average over all integers seen so far. - The solution should support efficient updates per element. Follow-up: - How would you modify the design if more recent numbers should have higher weight (e.g., exponentially decaying weights or a custom weight vector over the last N items)?

Quick Answer: This question evaluates understanding of streaming algorithms and sliding-window aggregation within the Coding & Algorithms domain, focusing on incremental computation, state management, and numerical stability when computing moving averages.

After each stream value, return the average of the last N values, or all seen values if fewer than N.

Examples

Input: (3, [1, 10, 3, 5])

Expected Output: [1.0, 5.5, 4.666667, 6.0]

Explanation: Window grows then slides.

Input: (1, [4, 5])

Expected Output: [4.0, 5.0]

Explanation: Only latest value.

Loading coding console...