FastPrepSliding Window Median
Problems/Sliding Window Median

Sliding Window Median

AuxoAI logoAuxoAI● HardFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This was one of four coding questions in an AuxoAI online assessment for an AI Engineer role. For every window of k numbers, you report its median. When k is even, the median is the smaller of the two middle numbers.

The post gives no limits and no examples, and it asked to print the medians. Here, you return them as an array. The array has up to 10^5 numbers between -10^9 and 10^9.

Our version is about 95% the same as the reported question.

You are given an integer array nums and an integer k.

A window is a block of k neighboring elements of nums. The first window starts at index 0, and each next window moves one step to the right, until the last window ends at the last index. So there are nums.length - k + 1 windows.

The median of a window is found by sorting its k numbers:

  • If k is odd, the median is the middle number.
  • If k is even, the median is the smaller of the two middle numbers.

Return an array with the median of every window, from the leftmost window to the rightmost.

Function

medianSlidingWindow(nums: int[], k: int) → int[]

Examples

Example 1

nums = [5,1,4,2,8,3]k = 3return = [4,2,4,3]

There are 6 - 3 + 1 = 4 windows. Sorted, they are:

  • [5,1,4] becomes [1,4,5], median 4.
  • [1,4,2] becomes [1,2,4], median 2.
  • [4,2,8] becomes [2,4,8], median 4.
  • [2,8,3] becomes [2,3,8], median 3.

Example 2

nums = [2,7,4,4,1]k = 2return = [2,4,4,1]

k = 2 is even, so each median is the smaller of the two numbers in the window.

The windows are [2,7], [7,4], [4,4] and [4,1], so the medians are 2, 4, 4 and 1.

Example 3

nums = [6,-2,9,0]k = 4return = [0]

The only window is the whole array. Sorted, it is [-2,0,6,9]. The two middle numbers are 0 and 6, and the smaller one is 0.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= k <= nums.length

More AuxoAI Online Assessment questions

  • Count Valid Square PairsOA · Seen Nov 2026
  • Largest Consistent Visiting GroupOA · Seen Nov 2026
  • Split Array Largest SumOA · Seen Nov 2026

Similar questions at other companies

  • Minimum Operations to Make an Array ContinuousAmazon · ONSITE INTERVIEW
  • Minimum Cost to Make Array EqualWalmart · OAWalmart interview questions
  • Find Array Uniqueness Median 🐈Microsoft · OAMicrosoft interview questions
  • Choose One from Each Set to Minimize the RangeInMobi · OAInMobi interview questions
  • Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
  • Linear InterpolatorTwo Sigma · OATwo Sigma interview questions
  • Maximum Requests in WindowSalesforce · OASalesforce interview questions
  • Minimum Parking Roof LengthAmerican Express · OAAmerican Express interview questions
  • Choose ContainersIMC Trading · OAIMC Trading interview questions
  • Minimize Maximum Group DifferenceDRW · OADRW interview questions
See AuxoAI hiring insights
Saved
class Solution {
    public int[] medianSlidingWindow(int[] nums, int k) {
        // Write your code here.
    }
}
Checking account…