KNOWLEDGE / Algorithms / Arrays
Sliding Window as a Reusable Pattern
How to recognize and reason about contiguous-range problems without memorizing solutions.
AlgorithmsArraysProblem Solving
- DOMAIN
- Data Structures & Algorithms
- LEVEL
- Foundational
- READ
- 6 min
- UPDATED
- Aug 7, 2026
MENTAL MODEL / KEY IDEAS
Keep these in mind
- 01The answer concerns a contiguous range
- 02Maintain an incremental invariant
- 03Move each boundary monotonically
Recognition
Sliding windows apply when the state of a contiguous range can be updated as one item enters and another leaves.
- Fixed-size windows
- Longest valid range
- Minimum satisfying range
The invariant
Define exactly what the current window stores and when it is valid. The loop then expands or contracts while preserving that rule.
- Counts
- Running sum
- Constraint violations
Complexity
When both pointers only move forward, each element enters and leaves at most once, producing linear time.