Quick Overview

This question evaluates proficiency in designing and analyzing efficient sliding-window algorithms and data structures for computing running medians, including correct handling of duplicates, deletions, and the convention for defining the median when k is even.

Compute sliding-window medians

Company: Meta

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given an array nums and an integer k, compute the median for each contiguous subarray (window) of length k and return the sequence of medians in order. Describe an algorithm running in O(n log k) time that handles duplicates and deletions efficiently, specify how to define the median when k is even, and analyze space complexity.

Overview: This question evaluates proficiency in designing and analyzing efficient sliding-window algorithms and data structures for computing running medians, including correct handling of duplicates, deletions, and the convention for defining the median when k is even.

Given an integer array `nums` and an integer `k`, compute the median of every contiguous subarray (window) of length `k`, and return the sequence of medians in order. The median is the middle value in an ordered window. When `k` is odd, it is the single middle element. When `k` is even, define it as the average of the two middle elements. Return each median as a floating-point number. **Approach for O(n log k):** Maintain a sorted multiset of the current window. For each step, remove the element leaving the window (binary-search its position to handle duplicates and deletions correctly) and insert the new element, then read the middle element(s). A balanced two-heap structure (a max-heap for the lower half and a min-heap for the upper half) with lazy deletion achieves the same O(log k) per slide. **Space complexity:** O(k) for the window structure plus O(n - k + 1) for the output sequence. Example: `nums = [1, 3, -1, -3, 5, 3, 6, 7]`, `k = 3` → windows are `[1,3,-1]`, `[3,-1,-3]`, `[-1,-3,5]`, `[-3,5,3]`, `[5,3,6]`, `[3,6,7]`, giving medians `[1.0, -1.0, -1.0, 3.0, 5.0, 6.0]`.

Constraints

  • 1 <= k <= nums.length
  • Window length k is fixed across all windows.
  • Array values may include duplicates and negative numbers.
  • Median for even k is the average of the two middle elements (returned as a float).
  • Return medians as floating-point values in window order.

Examples

Input: ([1, 3, -1, -3, 5, 3, 6, 7], 3)

Expected Output: [1.0, -1.0, -1.0, 3.0, 5.0, 6.0]

Explanation: Odd k=3: median is the middle element of each sorted window.

Input: ([1, 2, 3, 4], 2)

Expected Output: [1.5, 2.5, 3.5]

Explanation: Even k=2: each median is the average of the two window elements.

Hints

  1. Keep the current window in a sorted structure so the median is just the middle element(s) — index k//2 for odd k, average of indices k//2-1 and k//2 for even k.
  2. When sliding, binary-search for the outgoing element's exact position before removing it; this keeps duplicate handling correct (bisect_left finds the leftmost equal value).
  3. For a strict O(n log k) bound, use two balanced heaps (a max-heap for the lower half, a min-heap for the upper half) with lazy deletion, rebalancing so their sizes differ by at most one.

Sliding Window Median

mediumMetaMachine Learning Engineer

Given an integer array nums and an integer k, compute the median of every contiguous subarray (window) of length k, and return the sequence of medians in order.

The median is the middle value in an ordered window. When k is odd, it is the single middle element. When k is even, define it as the average of the two middle elements. Return each median as a floating-point number.

Approach for O(n log k): Maintain a sorted multiset of the current window. For each step, remove the element leaving the window (binary-search its position to handle duplicates and deletions correctly) and insert the new element, then read the middle element(s). A balanced two-heap structure (a max-heap for the lower half and a min-heap for the upper half) with lazy deletion achieves the same O(log k) per slide.

Space complexity: O(k) for the window structure plus O(n - k + 1) for the output sequence.

Example: nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3 → windows are [1,3,-1], [3,-1,-3], [-1,-3,5], [-3,5,3], [5,3,6], [3,6,7], giving medians [1.0, -1.0, -1.0, 3.0, 5.0, 6.0].

Examples

Example 1
Input
([1, 3, -1, -3, 5, 3, 6, 7], 3)
Output
[1.0, -1.0, -1.0, 3.0, 5.0, 6.0]
Notes
Odd k=3: median is the middle element of each sorted window.
Example 2
Input
([1, 2, 3, 4], 2)
Output
[1.5, 2.5, 3.5]
Notes
Even k=2: each median is the average of the two window elements.

Constraints

  • •1 <= k <= nums.length
  • •Window length k is fixed across all windows.
  • •Array values may include duplicates and negative numbers.
  • •Median for even k is the average of the two middle elements (returned as a float).
  • •Return medians as floating-point values in window order.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def medianSlidingWindow(nums: list[int], k: int) -> list[float]:
Sample call
medianSlidingWindow([1, 3, -1, -3, 5, 3, 6, 7], 3)
Language
Case 1
Input
([1, 3, -1, -3, 5, 3, 6, 7], 3)
Expected
[1.0, -1.0, -1.0, 3.0, 5.0, 6.0]
Case 2
Input
([1, 2, 3, 4], 2)
Expected
[1.5, 2.5, 3.5]

Browse More Questions