Skip to main content
Articles/sliding-window/monotonic-deque-unlocking-o-n-sliding-window-maximums
Scenario Article

Monotonic Deque: Unlocking O(N) Sliding Window Maximums

Struggling with slow O(NK) sliding window maximums? Master the monotonic deque for linear time efficiency!

Abstract Algorithmsβ€’Aug 27, 2026
XLinkedInReddit
Monotonic Deque: Unlocking O(N) Sliding Window Maximums
πŸ’‘ Executive Summary

πŸš€ The Monotonic Deque (Double-Ended Queue) is a powerful data structure that efficiently finds the maximum (or minimum) within a fixed-size sliding window in O(N) time, far superior to the naive O(NK) approach.

🎯 System Scenario & Requirements

System Goal

πŸš€ The Monotonic Deque (Double-Ended Queue) is a powerful data structure that efficiently finds the maximum (or minimum) within a fixed-size sliding window in O(N) time, far superior to the naive O(NK) approach.

Actors & Components

  • Application or API tier
  • Core service under design
  • Operator or SRE observer

Functional Requirements

  • πŸš€ The Monotonic Deque (Double-Ended Queue) is a powerful data structure that efficiently finds the maximum (or minimum) within a fixed-size sliding window in O(N) time, far superior to the naive O(NK) approach.
  • It's crucial for optimizing stream processing, financial analysis, and algorithmic challenges where window operations are frequent.
  • Imagine you're processing a continuous stream of data, like stock prices or sensor readings, and you need to find the maximum value within a fixed-size 'window' that slides across the data.

Constraints & Assumptions

  • Operational latency, consistency levels, and network partition boundaries must be enforced.

⚑ Execution Stages & Decision Trace

1

Stage 1 β€” Code Example: Sliding Window Maximum

Action / Decision:

Apply core-algorithm-patterns to Code Example: Sliding Window Maximum.

Component Behavior:

The components in β€œCode Example: Sliding Window Maximum” follow the mechanism explained in this stage.

Observable Output: Callers and operators can verify the stage outcome for β€œCode Example: Sliding Window Maximum”.
Read Full Stage in Deep Dive ↓
πŸ“–

Deep Dive

Detailed Architectural Analysis

Deep Dive: Stage 1 β€” Code Example: Sliding Window Maximum

from collections import deque

def max_sliding_window(nums, k):
    if not nums or k == 0:
        return []

    result = []
    # Deque stores indices
    # Elements are stored in decreasing order of their values
    dq = deque()

    for i in range(len(nums)):
        # 1. Remove elements from the front that are out of the current window
        if dq and dq[0] == i - k:
            dq.popleft()

        # 2. Remove elements from the back that are smaller than the current element
        #    These can no longer be the maximum because nums[i] is greater and appears later
        while dq and nums[dq[-1]] <= nums[i]:
            dq.pop()

        # 3. Add current element's index to the back of the deque
        dq.append(i)

        # 4. If the window is fully formed, the front of the deque is the maximum
        if i >= k - 1:
            result.append(nums[dq[0]])

    return result

# Example Usage:
nums = [1, 3, -1, -3, 5, 3, 6, 7]
k = 3
# Expected output: [3, 3, 5, 5, 6, 7]
print(f"Array: {nums}, Window Size: {k}")
print(f"Sliding Window Maximums: {max_sliding_window(nums, k)}")

nums_small = [1, 2, 3, 4, 5]
k_small = 2
# Expected output: [2, 3, 4, 5]
print(f"Array: {nums_small}, Window Size: {k_small}")
print(f"Sliding Window Maximums: {max_sliding_window(nums_small, k_small)}")

Observable outcome: You can explain how β€œCode Example: Sliding Window Maximum” changes the architecture or implementation path.

πŸ“Š System Outcome & Trade-offs

End-to-End Result: The system enforces causal consistency and bounded operations across distributed nodes.

Observed User & Operator Behavior

User: Clients observe deterministic state without lost updates or race conditions.

Operator: Operators can verify stage outcomes against the scenario requirements.

Key Architectural Trade-offs

  • System guarantees are traded against replication overhead and failover convergence times.
βœ…

Key learnings

  • Apply core-algorithm-patterns to Code Example: Sliding Window Maximum.
NEXT LEARNING STEPApply & Defend Decisions

Apply Concepts from this Scenario

Test your understanding of the architecture decisions and trade-offs presented in this case study.

Share this case study

Discuss architecture trade-offs and lessons with your team.

Share:XLinkedInReddit