When do you use the sliding window pattern?medium

Type
conceptual
Topic
sliding-window
Frequency
common
Roles
ml-engineer
Tags
array, string, sliding-window
Answer

Use sliding window for contiguous subarray or substring problems where you can update the answer as the window moves.

Explanation

Instead of recomputing each range from scratch, maintain state such as sum, counts, or distinct characters while expanding and shrinking the window. This often turns O(n^2) work into O(n).

Follow-upWhat is the difference between fixed-size and variable-size sliding windows?