Sliding Window Medians Problem

Sliding Window Medians Problem — ExecCode Hard DSA Practice

Solve the Sliding Window Medians problem on ExecCode. Free online hard DSA practice in Stacks & Queues. Write and run code in Java, C++, Python — no signup required to run.

Problem description

A stream window of width k slides across an array. After each placement you must report the median of the current window. You need a data structure that inserts, deletes the element leaving the window, and queries the median. Return an array of medians, one per window. For even k, average the two central values as a float. You are given: nums — integer stream k — window width, 1 <= k <= len(nums) Return list of floats of length n-k+1. The first median is for nums[0..k-1], the next for nums[1..k], and so on.

Examples

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]. Input {'nums': [1, 2, 3, 4], 'k': 2}; Output [1.5, 2.5, 3.5]. Input {'nums': [4], 'k': 1}; Output [4.0]

Constraints

Window must include exactly k consecutive elements Values may be negative

Practice Sliding Window Medians free on ExecCode. Browse DSA problems, topic map, and placement guides.