#239

Sliding Window Maximum

HardMonotonic DequeHas vizArrayQueueSliding WindowHeap

Problem

Sliding Window Maximum

You must solve the interview problem titled "Sliding Window Maximum".

Difficulty: Hard. Topics: Array, Queue, Sliding Window, Heap. Primary pattern: Monotonic Deque.

Read the title as the specification: implement the exact behavior it names, return the correct output type, and aim for an interview-optimal Monotonic Deque solution. Use the examples to lock input/output format, then confirm edge cases (empty, single element, duplicates, extremes).

Example 1

Input: sample input for Sliding Window Maximum

Output: sample output for Sliding Window Maximum

Apply a Monotonic Deque approach and match the required return type.

Example 2

Input: edge-case input for Sliding Window Maximum

Output: edge-case output for Sliding Window Maximum

Verify empty/single/boundary behavior.

Constraints

  • Interview-scale limits for a Hard Monotonic Deque problem.
  • Handle empty and single-element inputs when they are valid.
  • Prefer the optimal asymptotic complexity expected in interviews.
Loading visualization…

Explanation

Sliding Window Maximum is a Hard interview problem tagged [Array, Queue, Sliding Window, Heap]. The winning approach is typically **Monotonic Deque**.

Intuition

Each index enters/leaves once → O(n) sliding window maximum.

Approach

Deque stores indices in decreasing (or increasing) order so the front is the window extremum.

  1. Pop back while order breaks.
  2. Push current.
  3. Pop front if out of window.
  4. Front is the answer.

Solution

Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.

Pattern templateMonotonic Deque

Edge cases

  • •Empty or single-element input
  • •Duplicates and ties
  • •Maximum constraints / overflow
  • •Disconnected or unreachable states (for graphs)

Complexity

Time: O(n log n) – O(n²)

Space: O(n)

Before coding, restate the problem, name the pattern (Monotonic Deque), and write the invariant or DP state in one sentence.

Learn the topic

Got it?

Mark #239 Sliding Window Maximum as understood when the pattern clicks.