TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Simple Moving Average

Time Series
Easy

The Simple Moving Average (SMA) is the most basic time series smoothing technique. It computes the unweighted mean of a sliding window of consecutive observations, producing a smoother signal that filters out short-term fluctuations and highlights longer-term trends.

Given a list of numeric values and a window size k, compute the SMA for each valid window position.

Algorithm

For each position i from 0 to n - k, compute the average of k consecutive values:

SMA[i]=1k∑j=0k−1x[i+j]\text{SMA}[i] = \frac{1}{k} \sum_{j=0}^{k-1} x[i+j]SMA[i]=k1​j=0∑k−1​x[i+j]

The output has length n - k + 1, where n is the input length.

Return n minus window_size plus one moving-average values.

Loading visualization...

Examples

Input: values = [1, 2, 3, 4, 5], window_size = 3

Output: [2.0, 3.0, 4.0]

Explanation: The three complete windows have means 2, 3, and 4.

Input: values = [10, 20, 30, 40], window_size = 2

Output: [15.0, 25.0, 35.0]

Hint 1

Iterate over every start index that leaves a complete window.

Hint 2

Average the slice from the current start through window_size elements.

Requirements

  • Compute the arithmetic mean of each sliding window of size k
  • The output length should be n - window_size + 1
  • Return a list of floats representing the moving averages

Constraints

  • values has at least 1 element
  • 1 <= window_size <= len(values)
  • Return a list of floats
  • Time limit: 300 ms
Try Similar Problems
Weighted Moving AverageExponential Moving AverageMoving MedianLag FeaturesRolling Standard Deviation

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Simple Moving Average

Time Series
Easy

The Simple Moving Average (SMA) is the most basic time series smoothing technique. It computes the unweighted mean of a sliding window of consecutive observations, producing a smoother signal that filters out short-term fluctuations and highlights longer-term trends.

Given a list of numeric values and a window size k, compute the SMA for each valid window position.

Algorithm

For each position i from 0 to n - k, compute the average of k consecutive values:

SMA[i]=1k∑j=0k−1x[i+j]\text{SMA}[i] = \frac{1}{k} \sum_{j=0}^{k-1} x[i+j]SMA[i]=k1​j=0∑k−1​x[i+j]

The output has length n - k + 1, where n is the input length.

Return n minus window_size plus one moving-average values.

Loading visualization...

Examples

Input: values = [1, 2, 3, 4, 5], window_size = 3

Output: [2.0, 3.0, 4.0]

Explanation: The three complete windows have means 2, 3, and 4.

Input: values = [10, 20, 30, 40], window_size = 2

Output: [15.0, 25.0, 35.0]

Hint 1

Iterate over every start index that leaves a complete window.

Hint 2

Average the slice from the current start through window_size elements.

Requirements

  • Compute the arithmetic mean of each sliding window of size k
  • The output length should be n - window_size + 1
  • Return a list of floats representing the moving averages

Constraints

  • values has at least 1 element
  • 1 <= window_size <= len(values)
  • Return a list of floats
  • Time limit: 300 ms
Try Similar Problems
Weighted Moving AverageExponential Moving AverageMoving MedianLag FeaturesRolling Standard Deviation

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.