Sliding window
Understand fixed and variable sliding windows, identify the expand-right / shrink-left pattern, and recognise which problems call for it.
- Distinguish fixed-size and variable-size sliding windows
- Apply the expand-right / shrink-left loop structure to variable windows
- Identify window problems by their structure
A sliding window is a pair of indices [left, right] that together define
a contiguous range — a "window" — into an array or string. The window slides
across the input rather than restarting from scratch at every position, turning
many O(n²) nested-loop problems into O(n) ones.
Fixed-size windows
When the window size is constant (size k), both pointers advance together.
Add the incoming element on the right, remove the outgoing element on the left.
Canonical problem — maximum sum subarray of size k:
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]
best = max(best, window_sum)
return bestEach element enters and leaves the window exactly once: O(n). The naive approach
sums k elements at every position: O(nk).
Variable-size windows
When the constraint is something like "at most k distinct characters" or "no repeating characters," the window size varies. The pattern is:
- Expand right: advance
rightand incorporates[right]into the window's state. - Shrink left while invalid: if the window now violates the constraint,
advance
leftand removes[left]from state until the window is valid again. - Record the answer for the current valid window.
s = "a b c b d e"
L
R
If the constraint is "no repeating chars" and R just hit 'b' (already at L+1),
shrink L past the first 'b' to restore validity.Constraint as inequality: every variable window problem reduces to
maintaining a condition like invalid_chars == 0 or distinct_count <= k.
The shrink loop runs until that condition holds.
Recognising window problems
The structural tell-tale signs:
- The question asks for the longest or shortest contiguous subarray or substring satisfying some condition.
- The condition can be maintained incrementally — adding or removing one element updates it in O(1) or O(log n).
- The answer can be updated at each right-pointer position.
If you find yourself writing for i in range(n): for j in range(i, n):, stop
and ask whether a window approach can eliminate the inner loop.
Sliding window and two-pointer are closely related. A sliding window is essentially two pointers moving in the same direction (both left to right), with one leading and one trailing. "Two-pointer" usually refers to pointers moving in opposite directions from the two ends of the array.
Where to go next
Next: sliding window practice — three concrete implementations: max sum subarray (fixed), longest substring without repeating characters (variable), and minimum window substring.
Lab: graph algorithms
Three guided graph exercises — network delay time with Dijkstra's, course schedule ordering with Kahn's, and cycle detection for prerequisites.
Sliding window practice
Implement max sum subarray of size k, longest substring without repeating characters, and minimum window substring.