Preploop
Patterns
DSA Patterns
Sliding WindowSDE I
Maintain a window of elements — expand right, shrink left
O(n) O(1)
0/182
Problem

Find the maximum sum of any contiguous subarray of size k.

Visualizer step generation error. Reset inputs to run.
Controls
How to recognize this pattern
  • Problem asks about a CONTIGUOUS subarray or substring
  • Keywords: "maximum sum of k elements", "longest substring without repeating", "minimum window substring"
  • Brute force tries all O(n²) subarrays — you need O(n)
  • There is a clear "valid window" condition that changes as you slide
When to use this pattern

A nested O(n²) scan becomes O(n) when you realise you never need to recompute the window from scratch — just add one element and remove one.

  • Contiguous subarray/substring with a constraint (max sum, k distinct chars)
  • Fixed-size window: just slide (add right, drop left)
  • Variable-size window: expand until invalid, then shrink until valid
  • "Longest/shortest subarray that satisfies X"
1 / 1
Speed
Input