โ† Coding Interview Prep (LeetCode) learning path
Coding Interview Prep (LeetCode) ยท Module 5 of 10

Sliding window

A window moves across the data to track a range efficiently.

๐Ÿ“– Key ideas

  1. Fixed-size window: sum of every k consecutive elements in O(n).
  2. Variable window grows right and shrinks left while a condition breaks.
  3. Longest substring without repeating characters uses a set or map plus a window.
  4. Each element enters and leaves the window once, so time is O(n).
  5. Look for words like "subarray", "substring", "consecutive" and "at most k".
๐Ÿ’ก Real-life example: Like viewing a long train through a moving window: you see a few coaches at a time as it slides past.
WindowA continuous range of elements
ExpandMove the right edge forward
ShrinkMove the left edge forward
Fixed windowWindow of constant size k