LearnViz CompSci
Home / Learn / Algorithms / Sliding Window

Beginner Topic 17 of 51 Algorithms

Sliding Window

Maintaining a moving range to solve subarray problems in linear time.

How it works

The sliding-window technique maintains a contiguous range of a sequence and updates its summary, such as a sum, count or set of characters, incrementally as the window moves, instead of recomputing from scratch. Windows can be fixed in size or variable, expanding on the right and shrinking from the left to satisfy a condition. This turns many O(n times k) tasks into O(n).

Step-by-step walkthrough

  1. Find the best sum of 3 consecutive numbers.
  2. The first window 2 1 5 has sum 8.
  3. Slide right: add the new value 1 and subtract the old value 2 to get 7.
  4. Slide again: add 3 and drop 1 giving 9.
  5. The last slide gives 6, the best is 9 and the total work is O(n) instead of O(n k).

Where it is used

Maximum sum of k consecutive elements, longest substring without repeating characters, minimum window containing required characters, rate limiting over recent time, moving averages and network packet analysis use sliding windows.

Common pitfall

Incorrect window updates, such as forgetting to remove the element leaving the window, give wrong answers. It needs a monotone property: negative numbers can break shrink-on-condition logic. Use a hash map or deque to track the window contents, and test boundary windows.

Where it fits in the course

Sliding Window is topic 17 of 51 in Algorithms, rated Beginner. It follows Two Pointers. The next topic is Heap Sort.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Sliding Window?
Maintaining a moving range to solve subarray problems in linear time. It belongs to Algorithms and is a beginner-level topic.
Where do people use Sliding Window?
Maximum sum of k consecutive elements, longest substring without repeating characters, minimum window containing required characters, rate limiting over recent time, moving averages and network packet analysis use sliding windows.
What is the most common mistake with Sliding Window?
Incorrect window updates, such as forgetting to remove the element leaving the window, give wrong answers.
What should I learn before this topic?
Start with Two Pointers, the topic just before it in Algorithms.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Algorithms quiz.
Is Sliding Window hard to learn?
It is a Beginner topic, so it works as an entry point. If the terms are new, read the explanation twice and try the visualizer before the quiz.
How long does it take to learn Sliding Window?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Sliding Window?
Two Pointers, Heap Sort, Brute Force are closely connected. The full list is in the related topics section of this page.
What should I study after Sliding Window?
Continue with Heap Sort, the next topic in Algorithms.

Related topics