โ Coding Interview Prep (LeetCode) learning pathCoding Interview Prep (LeetCode) ยท Module 5 of 10
Sliding window
A window moves across the data to track a range efficiently.
๐ Key ideas
- Fixed-size window: sum of every k consecutive elements in O(n).
- Variable window grows right and shrinks left while a condition breaks.
- Longest substring without repeating characters uses a set or map plus a window.
- Each element enters and leaves the window once, so time is O(n).
- 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