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

Moving Median

Time Series
Easy

A moving median smooths a sequence by taking the median of every complete sliding window. Sorting a window places its middle value or values at known positions.

For an odd window size, use the single middle value. For an even window size, use the average of the two middle values. Return one floating-point median for each complete window.

Loading visualization...

Examples

Input: values = [1, 3, 5, 7, 9], window_size = 3

Output: [3.0, 5.0, 7.0]

Explanation: The middle values of the three sorted windows are 3, 5, and 7.

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

Output: [1.5, 2.5, 3.5]

Hint 1

Use sorted on each slice of length window_size.

Hint 2

The middle index is window_size // 2.

Requirements

  • Sort each complete sliding window.
  • Use the middle value for an odd window size.
  • Average the two middle values for an even window size.
  • Return len(values) - window_size + 1 floats.

Constraints

  • 1 <= window_size <= len(values).
  • values contains numeric values.
  • Time limit: 300 ms.
Try Similar Problems
Simple Moving AverageWeighted Moving AverageExponential Moving AverageRolling Standard DeviationLag Features

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Moving Median

Time Series
Easy

A moving median smooths a sequence by taking the median of every complete sliding window. Sorting a window places its middle value or values at known positions.

For an odd window size, use the single middle value. For an even window size, use the average of the two middle values. Return one floating-point median for each complete window.

Loading visualization...

Examples

Input: values = [1, 3, 5, 7, 9], window_size = 3

Output: [3.0, 5.0, 7.0]

Explanation: The middle values of the three sorted windows are 3, 5, and 7.

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

Output: [1.5, 2.5, 3.5]

Hint 1

Use sorted on each slice of length window_size.

Hint 2

The middle index is window_size // 2.

Requirements

  • Sort each complete sliding window.
  • Use the middle value for an odd window size.
  • Average the two middle values for an even window size.
  • Return len(values) - window_size + 1 floats.

Constraints

  • 1 <= window_size <= len(values).
  • values contains numeric values.
  • Time limit: 300 ms.
Try Similar Problems
Simple Moving AverageWeighted Moving AverageExponential Moving AverageRolling Standard DeviationLag Features

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.