LeetEye LeetEye
Pattern

Sliding Window

Optimize subarray and substring problems with the sliding window pattern.

6 Problems
0 Easy
6 Medium
0 Hard

How Sliding Window Works

Sliding Window pattern visualization

Sliding Window maintains a dynamic window (subarray) that expands and contracts as it moves through the array. You grow the window by advancing the right pointer and shrink it by advancing the left pointer. The window tracks a running state (sum, character count, etc.) that updates incrementally instead of recalculating from scratch. This converts O(n×k) brute-force approaches to O(n) by reusing computation from the previous window position.

When to Use Sliding Window

Pattern Recognition

Look for these trigger words in problem statements:

best time to buy and sell stock sliding-window longest substring without repeating characters longest repeating character replacement permutation in string minimum window substring sliding window maximum

Common Mistakes

  • Shrinking the window too aggressively — only shrink when the constraint is violated
  • Not updating the window state correctly when removing the left element
  • Confusing fixed-size windows (always size k) with variable-size windows (expand/shrink)
  • Forgetting to check the result after the final expansion, missing the last valid window

When NOT to Use Sliding Window

  • When elements are not contiguous (use dynamic programming or backtracking)
  • When you need to compare non-adjacent elements (use two pointers)
  • When the problem requires considering all subsequences, not just subarrays

Practice Problems

Master Sliding Window

Build pattern recognition with interactive MCQs. Understand why to use Sliding Window, not just how.

Download LeetEye Free
Practice in LeetEye