← BACK TO KNOWLEDGE

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

  1. 01The answer concerns a contiguous range
  2. 02Maintain an incremental invariant
  3. 03Move each boundary monotonically
01

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
02

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
03

Complexity

When both pointers only move forward, each element enters and leaves at most once, producing linear time.