KatInterview

Rolling Window Median Bounds + Binary Array Tiling

Heaps, Sorting & SearchModel answer
HardModel answerHeaps, Sorting & Searchsliding windowheapdynamic programming

Last updated

Find the max and min sliding-window medians in O(n log k), then partition a binary array into even uniform segments with minimum flips.

Part A — Min and Max of All Window Medians

You are given an integer array nums of length n and a window size k (1 ≤ k ≤ n).

Slide a window of exactly k elements across nums from left to right. For each position, compute the lower median of the elements in that window — that is, the element at 0-indexed sorted position (k - 1) // 2. Once you have all those medians, return their maximum and their minimum.

Requirements:

  • Time complexity: O(n log k)
  • Space complexity: O(k)
  • Describe the data structures you use and justify both bounds.

Example:

nums = [3, 1, 4, 1, 5, 9, 2], k = 3
Windows:     [3,1,4] → 3,  [1,4,1] → 1,  [4,1,5] → 4,  [1,5,9] → 5,  [5,9,2] → 5
Output: max = 5, min = 1

Part B — Even-Length Uniform Tiling with Fewest Flips

You are given a binary array frames of length n (each element is 0 or 1).

You may flip any elements (0 to 1, or 1 to 0). After flipping, the array must be partitioned into contiguous segments where every segment has even length and every element in a segment is identical (all 0s or all 1s). Adjacent segments may have different values.

Goals, in priority order:

  1. Minimize the number of segments.
  2. Subject to that minimum, minimize the total number of flips.

Return the minimum segment count and the minimum number of flips needed to achieve it.

Requirements:

  • Describe your algorithm and state its time and space complexity.
  • Handle edge cases: odd n, empty array, already-uniform arrays.

Example:

frames = [1, 1, 0, 1, 0, 0]
Pairs: (1,1), (0,1), (0,0)
Pair 0 is already uniform (value 1, 0 flips).
Pair 1 is mixed — flip to 1 (1 flip) to stay in the same segment, or flip to 0 (1 flip) to start a new segment.
Pair 2 is already uniform (value 0).
Optimal: [1,1,1,1,0,0] → 2 segments, 1 flip.
Output: (2, 1)

Hints

Hint 1

For Part A, think about what structure lets you read off the median in O(1). A max-heap holding the smaller half and a min-heap holding the larger half keeps the median at the top of the max-heap — but how do you remove the element that leaves the window without breaking the heap property?

Hint 2

For Part A's deletion problem, you don't have to remove stale elements immediately. Instead, mark them and skip them when they surface at the top. Track valid counts separately from physical heap sizes so rebalancing uses real counts. To keep space O(k) rather than O(n), periodically strip out stale entries and re-heapify.

Hint 3

For Part B, notice that every segment boundary must fall on an even index, so the array naturally breaks into consecutive non-overlapping pairs. Each pair must become uniform independently. The number of segments is 1 plus the number of adjacent pairs that end up with different values — a classic DP transition over two states (the value assigned to the current pair).