Problem · Array

Fixed-Window Rolling Mean

Learn this problem
EasyJump Trading logoJump TradingFULLTIMEONSITE INTERVIEW

Problem statement

Given an integer array nums and a positive windowSize, return the arithmetic mean of every complete contiguous window of that size, from left to right.

Update the running window in constant time as it advances.

Function

fixedWindowRollingMean(nums: int[], windowSize: int) → double[]

Examples

Example 1

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]windowSize = 7return = [4.0, 5.0, 6.0]

The three complete windows have sums 28, 35, and 42; dividing each by 7 gives the returned means.

Constraints

  • nums.length >= 1
  • 1 <= windowSize <= nums.length
  • The result contains exactly nums.length - windowSize + 1 averages, one for each contiguous window.

More Jump Trading problems

drafts saved locally
public double[] fixedWindowRollingMean(int[] nums, int windowSize) {
  // write your code here
}
nums[1, 2, 3, 4, 5, 6, 7, 8, 9]
windowSize7
expected[4.0, 5.0, 6.0]
checking account