FastPrepRolling Median Absolute Deviation
Problems/Rolling Median Absolute Deviation

Rolling Median Absolute Deviation

Datadog logoDatadog● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The firsthand DS interview report asks for the MAD of every rolling window, then discusses avoiding repeated sorting. This exercise specifies unscaled MAD, the usual averaged even median, complete-window behavior, callable and bounds. The core task matches the report at about 95%.

For each consecutive window of exactly k values, return its unscaled median absolute deviation (MAD). First find the window median m, then take the median of the k values abs(x - m).

For an odd number of values, the median is the middle sorted value. For an even number, it is the arithmetic mean of the two middle sorted values. Apply this same rule to the original values and to the deviations. Do not multiply by a normalization factor.

Return the MAD values in increasing window-start order. If fewer than k values are available, including an empty input, return an empty array. Do not mutate the input.

Function

rollingMad(values: int[], k: int) → double[]

Examples

Example 1

values = [1,2,10,4]k = 3return = [1.0,2.0]

The windows are [1,2,10] and [2,10,4]. Their medians are 2 and 4. The sorted deviations are [0,1,8] and [0,2,6], so their MAD values are 1 and 2.

Example 2

values = [1,2,8,9]k = 4return = [3.5]

The median is (2+8)/2=5. The sorted absolute deviations are [3,3,4,4], whose median is 3.5.

Example 3

values = [5,5]k = 3return = []

There are only two values, so no complete window of three exists.

Constraints

  • 0 <= values.length <= 500
  • -1000000 <= values[i] <= 1000000
  • 1 <= k <= 500

More Datadog problems

  • Query Log MatchPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Delete a Filesystem TreePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Grouped-Query Attention Forward PassPHONE SCREEN · Seen Jun 2026
All Datadog interview questionsSee Datadog hiring insights
Saved
public double[] rollingMad(int[] values, int k) {
    // Write your code here.
}
Checking account…