PracHub
QuestionsCoachesLearningGuidesInterview Prep

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.

  • Medium
  • Meta
  • Coding & Algorithms
  • Machine Learning Engineer

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.

Quick Answer: 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.

Input: ([5], 1)

Expected Output: [5.0]

Explanation: Single-element window; the median is the element itself.

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

Expected Output: [2.0, 2.0, 2.0]

Explanation: All duplicates — removal must target one equal value, not all; every window median is 2.

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

Expected Output: [-2.5, -2.5]

Explanation: Even k=4 with negatives: window [-5,-3,-1,-2] sorts to [-5,-3,-2,-1], middle two are -3 and -2 → -2.5; next window [-3,-1,-2,-4] sorts to [-4,-3,-2,-1] → -2.5.

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

Expected Output: [3.5]

Explanation: Whole array is a single even-length window; median is average of 3 and 4.

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.
Last updated: Jun 25, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)