Asked at

Moving Average From Data Stream

Easy
Verified
QueueSliding Window~15 min

Push each value of nums into a sliding window of capacity size, returning the running average after every push.

Before the window fills, average over the values seen so far. A queue keeps the window in O(1) per push.

The input arrives as a single object { size, nums }.

Examples

in{ size: 3, nums: [1, 10, 3, 5] }
out[1, 5.5, 4.667, 6]

Each push averages the last up-to-3 values: 1, (1+10)/2, (1+10+3)/3, (10+3+5)/3.

in{ size: 1, nums: [4, 5, 6] }
out[4, 5, 6]

A window of 1 returns each value unchanged.

Constraints

  • 1 ≤ size ≤ 1000
  • -10⁵ ≤ nums[i] ≤ 10⁵
  • 0 ≤ nums.length ≤ 10⁴

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.