SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Singly Linked ListReverse Linked ListDoubly Linked ListData-Structure and Linked-List Trade-offsMerge Two Sorted ListsRemove Nth Node From End of ListStackValid ParenthesesMin StackDaily TemperaturesQueueImplement Queue Using StacksCircular QueuesCircular Queues QuizDesign Circular QueueSliding Window MaximumQueues, Circular Buffers, and Monotonic Deques
Linked Lists, Stacks & Queues

Sliding Window Maximum

Maintain a monotonic deque to report every window maximum.

Try It Yourself

Return the maximum for each window of k consecutive elements. As the window moves one step, one value enters and another expires.

Track maximum candidates in a deque

A deque (double-ended queue) lets you remove candidates from either end. Remove indices that leave the window and candidates that can no longer become a maximum.

Submit your implementation when it passes the examples.

Sliding Window Maximum

hardDeque · Sliding Window · Monotonic Queue40 minLC 239
Problem

Given an integer array and a window size k, return the maximum value in every contiguous window of size k.

Process the array efficiently without scanning every window from scratch.

Example 1:

Input: nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
Output: [3, 3, 5, 5, 6, 7]
Explanation: The first two windows, [1, 3, -1] and [3, -1, -3], both have maximum 3. The next four windows have maxima 5, 5, 6, and 7.

Example 2:

Input: nums = [1], k = 1
Output: [1]
Explanation: The only window contains 1 by itself, so its maximum is 1.

Constraints

  • 1 ≤ k ≤ nums.length ≤ 100,000
  • -10,000 ≤ nums[i] ≤ 10,000
0 attempts

Test results

Results appear here after a run.

Solution

For windows containing 4, 2, 3 and then 2, 3, 1, the deque keeps the candidates 4, 3 and then 3, 1. Each first candidate is the window maximum.

Store indices in increasing order from front to back. Their values must strictly decrease in that direction. With current index i, first remove indices at the front that are at most i - k. Next remove indices at the back whose values are at most nums[i]. The newer value is at least as large and stays in the window longer. Append i. Once the window is full, read the value at the front index.

For [4, 2, 3, 1] with k = 3, the arrival of 3 discards the older 2. The candidates are now indices [0, 2] with values [4, 3], so the first maximum is 4. On the next step, index 0 expires. The candidates become [2, 3], with values [3, 1], so the second maximum is 3.

function maxSlidingWindow(nums, k) {
  const entries = {};
  let front = 0;
  let back = 0;
  const result = [];

  for (let index = 0; index < nums.length; index++) {
    while (front < back && entries[front] <= index - k) {
      delete entries[front];
      front++;
    }

    while (
      front < back &&
      nums[entries[back - 1]] <= nums[index]
    ) {
      back--;
      delete entries[back];
    }

    entries[back] = index;
    back++;

    if (index >= k - 1) {
      result.push(nums[entries[front]]);
    }
  }

  return result;
}

Big O notation

MeasureComplexityExplanation
TimeO(n)Each index enters and leaves the deque at most once.
Auxiliary spaceO(k)At most k candidate indices are live. The returned array separately holds n - k + 1 maxima.

How is this practice?

Previous lessonDesign Circular QueueNext lessonQueues, Circular Buffers, and Monotonic Deques
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation