Sliding Window Median - Problem
Sliding Window Median is a challenging problem that combines data streaming with efficient median calculation.

Given an integer array nums and window size k, imagine a sliding window moving from left to right across the array. At each position, you need to find the median of the k numbers currently in the window.

๐Ÿ” What's a median?
โ€ข For odd-sized windows: the middle element when sorted
โ€ข For even-sized windows: average of the two middle elements

Example: If nums = [1,3,-1,-3,5,3,6,7] and k = 3:
โ€ข Window [1,3,-1] โ†’ sorted: [-1,1,3] โ†’ median = 1
โ€ข Window [3,-1,-3] โ†’ sorted: [-3,-1,3] โ†’ median = -1
โ€ข Window [-1,-3,5] โ†’ sorted: [-3,-1,5] โ†’ median = -1

Your task is to return an array containing the median for each window position. This problem tests your ability to efficiently maintain sorted order in a dynamic sliding window.

Input & Output

example_1.py โ€” Basic sliding window
$ Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
โ€บ Output: [1.0, -1.0, -1.0, 3.0, 5.0, 6.0]
๐Ÿ’ก Note: Window positions and their medians: [1,3,-1]โ†’1, [3,-1,-3]โ†’-1, [-1,-3,5]โ†’-1, [-3,5,3]โ†’3, [5,3,6]โ†’5, [3,6,7]โ†’6
example_2.py โ€” Even-sized window
$ Input: nums = [1,2,3,4,2,3,1,4,2], k = 4
โ€บ Output: [2.5, 3.0, 3.0, 3.0, 2.5, 3.0]
๐Ÿ’ก Note: Even k means median is average of two middle elements: [1,2,3,4]โ†’(2+3)/2=2.5, [2,3,4,2]โ†’(2+3)/2=2.5, etc.
example_3.py โ€” Single element window
$ Input: nums = [1,4,2,3], k = 1
โ€บ Output: [1.0, 4.0, 2.0, 3.0]
๐Ÿ’ก Note: When k=1, each element is its own median: window [1]โ†’1, [4]โ†’4, [2]โ†’2, [3]โ†’3

Visualization

Tap to expand
MAX HEAP (Smaller Half)31-1-30MIN HEAP (Larger Half)56789MEDIAN(3+5)/2= 4.0โšก O(1) Median Access๐Ÿ”„ Rebalance keeps heaps within size difference of 1
Understanding the Visualization
1
Initialize Balance
Set up two heaps: left side (max-heap) for smaller values, right side (min-heap) for larger values
2
Add New Element
Compare incoming element with heap tops and add to appropriate side
3
Maintain Balance
If one side gets too heavy (size difference > 1), move element to other side
4
Remove Old Element
Mark outgoing elements for lazy deletion to avoid expensive heap operations
5
Extract Median
For odd k: take top of larger heap. For even k: average both heap tops
Key Takeaway
๐ŸŽฏ Key Insight: By maintaining two balanced heaps (one for smaller half, one for larger half), we achieve O(1) median access while keeping insertion/deletion at O(log k). The balance is the secret - it ensures median elements are always at the heap tops!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log k)

n elements, each heap operation takes O(log k)

n
2n
โšก Linearithmic
Space Complexity
O(k)

Two heaps store at most k elements total, plus hash map for lazy deletion

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค k โ‰ค nums.length โ‰ค 105
  • -231 โ‰ค nums[i] โ‰ค 231 - 1
  • Answers within 10-5 of actual value will be accepted
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
52.0K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen