Skip to main content
← PROBLEMS / CODING-ALGORITHM
An end-to-end domain learning journey

Eliminating O(N*k) TLE & Lazy Heap
Bloat in Sliding Window Extrema.

Rescanning k-element windows triggers 10^11 ops (TLE) at N=10^6, while lazy max-heaps bloat to O(N) RAM on decreasing streams.
Start the journey3 stages· Last modified Nov 28, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When solving sliding window extrema under tight memory/latency budgets, use an index-based Monotonic Deque.

TL;DR30-second simplified summary
▾

Each index is pushed once and popped at most once, giving O(N) total time and O(k) space.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
Sliding window index bounds [i - k + 1, i]
Core prerequisite: Sliding window index bounds [i - k + 1, i].
Heap lazy deletion failure modes
Core prerequisite: Heap lazy deletion failure modes.
Full walkthrough · 3 stages
01
STAGE 01L4 · FUNDAMENTALS

Why Lazy Heaps Fail on Decreasing Streams.

If nums = [N, N-1, ..., 1], the root is popped only once per step while pushed elements accumulate, leaving O(N) stale nodes in memory.

Before · Naive
After · Hardened
02
STAGE 02L4 · FUNDAMENTALS

Zero-Allocation Java Ring Deque.

Use a preallocated primitive int[] array in Java to avoid Integer boxing overhead under 1M+ samples/sec.

int[] dq = new int[n];
int head = 0, tail = 0;
for (int i = 0; i < n; i++) {
    while (head < tail && dq[head] < i - k + 1) head++;
    while (head < tail && nums[dq[tail - 1]] <= nums[i]) tail--;
    dq[tail++] = i;
    if (i >= k - 1) res[i - k + 1] = nums[dq[head]];
}
03
STAGE 03L5 · APPLIED DESIGN

Boundary & Edge-Case Invariants.

Handle k = 1, k > n, and all-equal arrays cleanly by checking i >= k - 1 after both eviction loops. [INSERT YOUR BENCHMARK / PROD NOTE] for throughput.

Window Invariant & Bounds

Boundary & Edge-Case Invariants

Before · Unbounded / Out-of-Order Checks
  • Reads expired dq[head] if checked late
  • O(k) duplicates when using strict <
After · Strict Pre-Emit Eviction
  • Expire front (< i - k + 1) before read
  • Emit only when i >= k - 1 (handles k=1, k>n)
Engineering Decision Framework
Problem
Maintaining exact sliding-window maxima or minima at 1M+ samples/sec under strict latency and O(k) memory bounds.
Naive Solution
Rescan each k-element window in O(N*k) time or push every sample into a lazy-deletion Max-Heap / PriorityQueue.
Why It Fails
Rescanning hits 10^11 ops (TLE) at N=10^6, while lazy heaps accumulate O(N) stale nodes beneath valid maxima on decreasing or skewed streams and pay O(log N) per push.
Alternatives
Index-based Monotonic Deque (O(N) time, O(k) space), Segment Tree / Sparse Table (O(N log N) or O(N) build with higher memory), or dual-heap for non-monotonic order statistics.
Engineering Decision
Use an index-based Monotonic Decreasing Deque (backed by a primitive int[] ring buffer in Java) whenever the aggregate is monotonic (max/min); reserve heaps or balanced trees for non-monotonic aggregates like rolling median.
Trade-Offs
Guarantees O(1) amortized time per element and <= k live entries with zero Integer autoboxing, at the cost of only supporting monotonic extrema rather than arbitrary rank queries.
Decision Exercise · Make the Call

What happens if you check front expiration AFTER reading nums[dq[0]] instead of before?

ChoiceWhat it buysWhat it costs
Strictly Decreasing StreamMonotonic Deque stays O(k) space and O(N) timeLazy Max-Heap bloats to O(N) stale nodes and O(N log N) time
Strictly Increasing StreamBoth Lazy Max-Heap and Monotonic Deque stay O(1) live sizeLazy Max-Heap still pays O(log k) heapify overhead per push
All-Equal StreamMonotonic Deque with <= eviction stays O(1) sizeLazy Max-Heap retains up to O(k) duplicate entries in window
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 2
Score: 0/2

On a strictly decreasing array of length N with window k, what is the peak size of a lazy Max-Heap?

Recommended Learning Flow