view_weekSliding Window

Sliding Window: when to spot it, explain it, and practice it

Sliding window is not just a template. It is a way to turn brute-force range enumeration into a controlled expand-and-shrink process, which is why it dominates substring and subarray interview rounds.

Pattern coverage

50+

Best first move

Decide whether the window is fixed-size or variable-size before coding.

Common failure point

Shrinking on the wrong condition and discarding valid windows too early.

When this pattern should come to mind

The problem asks about a contiguous range, substring, or subarray.
You need the longest, shortest, or count of ranges satisfying a condition.
Brute force looks like checking every interval and feels too expensive.

Checklist before you code

Decide whether the window is fixed-size or variable-size before coding.
Write down what state enters the window and what state leaves it.
Be explicit about the condition that makes the window valid or invalid.
Only update the answer when the window is in the state you actually need.

The solving flow that works well in interviews

1

Confirm that the answer can be derived from a moving contiguous interval.

2

Choose the window state: sum, distinct count, frequency, or matched characters.

3

Expand the right pointer and update the state immediately.

4

Shrink from the left only while the window is invalid or oversized.

5

Update the answer with the valid window that remains.

Common variants

Fixed-size window

Best when the window length is given directly, such as all substrings of length k.

Variable-size window

Best when the condition depends on counts, sums, or coverage and the left boundary moves only when invalid.

Window + frequency map

Useful when repeated characters or repeated values define validity.

Template preview

PythonPublic preview
# Variable size sliding window
left = 0
window = {}
answer = 0

for right, value in enumerate(nums):
    add(window, value)
    while not is_valid(window):
        remove(window, nums[left])
        left += 1
    answer = update(answer, left, right, window)

# Fixed size sliding window
window = initialize(nums[:k])
answer = evaluate(window)
for right in range(k, len(nums)):
    remove(window, nums[right - k])
    add(window, nums[right])
    answer = update(answer, right - k + 1, right, window)

A more useful problem ladder for practice

This is not a random list. It is ordered to help candidates build recognition first, add key variants next, and then increase pressure with harder cases.

High-frequency mistakes

warning

Shrinking on the wrong condition and discarding valid windows too early.

warning

Updating the answer before the window becomes valid.

warning

Forgetting to remove state when the left pointer moves.

warning

Mixing fixed-size and variable-size logic in the same loop.

Recommended practice path

1

Start with fixed-size frequency problems such as permutation matching.

2

Move to basic variable windows like minimum size subarray sum.

3

Then solve coverage windows such as minimum window substring.

4

Finally revisit harder hybrids that combine window logic with maps and counts.

Sliding Window Pattern Guide | LeetCode Interview Prep - Interview AiBox