Browse Curriculum

The Sliding Window Pattern

A window defined by a left and right index moves across the data. The right edge extends to include new elements; the left edge advances only when the window breaks a constraint. Running totals update incrementally rather than being recomputed, so each element is visited a constant number of times.

6
Interactive problems
2
Free to open

How to recognise a sliding window problem

The problem asks for the longest, shortest, or best contiguous subarray or substring satisfying a condition. The word "contiguous" is the tell, as is a fixed window size k. "Longest substring without repeating characters" and "maximum sum subarray of size k" are the canonical forms.

Time and space complexity

O(n) time — each index enters and leaves the window at most once — against O(n²) or O(n³) for recomputing every subarray.

Sliding Window practice problems

Each problem pairs a worked explanation with an interactive visualizer you step through yourself, plus the implementation in JavaScript, Python, Java and C++.


Related patterns and concepts

The underlying technique is covered from first principles in the sliding window lesson in the DSA visualizer curriculum. Sliding Window is one of the 16 coding interview patterns, and its problems also appear in the DSA 75 interview sprint.