By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
When you need to find the longest substring without repeating characters, or the maximum sum of a contiguous subarray of fixed length, a naive approach recalculates the entire window for each position. That's O(n²) and wasteful. The sliding window pattern maintains a range that moves through the array once, updating the window state incrementally. You get O(n) instead.
A sliding window is just a range [left, right] that you expand and contract as you iterate through the array. When you move right forward to include a new element, you update your aggregate (sum, count, set of characters) by adding that element. When you move left forward to exclude an element, you update the aggregate by removing it. This incremental update is the key to avoiding redundant work.
There are two main flavors: fixed-size windows, where the range stays constant width, and variable-size windows, where the width changes based on some constraint.
Suppose you want the maximum sum of any contiguous subarray of exactly k elements. The brute force recalculates the sum for each k-length window, which is wasteful.
def max_sum_subarray(nums, k):
window_sum = sum(nums[:k])
best = window_sum
for i in range(k, len(nums)):
window_sum += nums[i] - nums[i - k] # slide
best = max(best, window_sum)
return best
print(max_sum_subarray([2,1,5,1,3,2], 3)) # 9 (5+1+3)The pattern here is straightforward. Compute the sum of the first k elements. Then slide the window one position at a time: add the new element entering on the right, subtract the element leaving on the left. Track the maximum you see. Each element is touched exactly twice—once when it enters, once when it leaves—so the total work is O(n).
Now suppose you want the longest substring with no repeating characters. The window size varies depending on where the repeated character is. You expand right to include new characters. When you encounter a repeat, you shrink from the left until the repeat is gone.
def length_of_longest_substring(s):
seen = set()
left = best = 0
for right in range(len(s)):
while s[right] in seen:
seen.remove(s[left])
left += 1
seen.add(s[right])
best = max(best, right - left + 1)
return best
print(length_of_longest_substring("abcabcbb")) # 3The right pointer always moves forward through the string exactly once. The left pointer also moves forward exactly once (never backward). This means both pointers traverse the string a single time, giving O(n). The set tracks which characters are currently in the window. When right lands on a character already in the set, you remove characters from the left until that character is gone, then add the new character.
Sliding window applies when you're searching for a subarray or substring that satisfies some constraint. Common signals: you need the longest or shortest subarray/substring with property X, the maximum sum of k consecutive elements, or the count of subarrays matching a condition. If the brute force involves nested loops over subarrays, consider whether you can maintain state incrementally instead.
The pattern breaks down if the constraint is not local to the window—for instance, if you need to compare elements far apart in the array. It also requires that expanding the window monotonically helps you make progress toward the answer, which is true for most substring and subarray problems but not all.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.