Sliding Window Technique
Channel: Profound Academy
Watch on YouTube →Algorithm • medium
Sliding window maintains a moving contiguous range while updating state incrementally.
It is the go-to strategy for substring/subarray constraints where you need best/min/max window under rules.
The key is deterministic add-right/remove-left updates so each index is touched a constant number of times.
Typical Complexity Baseline
| Metric | Value |
|---|---|
| Typical traversal | O(n) |
See It
A small animated SVG that illustrates the core idea the moving parts you need to keep in your head when you read the rest of the guide.
The window slides one step at a time, adding the new element and dropping the old one.
Watch First
A quick visual walkthrough before the detailed sections pick the format that helps you learn fastest.
Channel: Profound Academy
Watch on YouTube →Mechanics
The building blocks and terminology you need before everything else clicks. Skim or deep-read.
What it is Left and right pointers defining active range.
Why it matters Window boundaries is a required building block for understanding how Sliding Window stays correct and performant on large inputs.
What it is Include right element and update state.
Why it matters Add step is a required building block for understanding how Sliding Window stays correct and performant on large inputs.
What it is Move left boundary until constraints restore.
Why it matters Shrink step is a required building block for understanding how Sliding Window stays correct and performant on large inputs.
What it is Counts/sums/sets needed for validity checks.
Why it matters State tracker is a required building block for understanding how Sliding Window stays correct and performant on large inputs.
What it is Capture optimal window length/value when valid.
Why it matters Best answer update is a required building block for understanding how Sliding Window stays correct and performant on large inputs.
What it is Window size is constant (e.g., length k).
Why it matters Window size is constant (e.g., length k). In Sliding Window, this definition helps you reason about correctness and complexity when inputs scale.
What it is Window expands/shrinks based on validity condition.
Why it matters Window expands/shrinks based on validity condition. In Sliding Window, this definition helps you reason about correctness and complexity when inputs scale.
What it is Constraint currently satisfied for active range.
Why it matters Constraint currently satisfied for active range. In Sliding Window, this definition helps you reason about correctness and complexity when inputs scale.
What it is Each index enters/leaves window at most once.
Why it matters Each index enters/leaves window at most once. In Sliding Window, this definition helps you reason about correctness and complexity when inputs scale.
What it is Character/value counts used to evaluate window constraints.
Why it matters Character/value counts used to evaluate window constraints. In Sliding Window, this definition helps you reason about correctness and complexity when inputs scale.
Walkthrough
Every frame of the concept diagram, laid out top-to-bottom so you can scroll through the algorithm at your own pace.
Initial window [0..2] sum = 8.
Slide right. drop 4, add 7. window sum = 11. best = 11.
Slide right. drop 1, add 2. window sum = 12. best = 12.
Slide right. drop 3, add 5. window sum = 14. best = 14.
Slide right. drop 7, add 8. window sum = 15. best = 15.
Slide right. drop 2, add 1. window sum = 14. best = 15.
Tradeoffs
When this technique wins, when it loses, and what to reach for instead.
Choose this when Choose sliding window when constraints depend on mutable content inside range.
Tradeoff Prefix sums are better for static sum queries.
Choose this when Choose sliding window for contiguous range optimization.
Tradeoff General two pointers may be simpler for pairwise non-window problems.
Choose this when Choose sliding window to avoid O(n^2) range enumeration.
Tradeoff Needs careful state update design.
In Production
Where this shows up at real companies and what the on-call engineer learned the hard way.
Monitoring alerts evaluate rolling windows over metrics streams to detect spikes and anomalies.
Takeaway Sliding windows are practical for continuous observability workloads.
Rate-limiting systems apply per-key request windows to enforce fair usage.
Takeaway Window-based constraints protect services under burst traffic.
Session analytics often rely on rolling windows for engagement and retention signals.
Takeaway Window logic underpins many product analytics metrics.
Code
A clean reference implementation in TypeScript, Python, and Go. Switch languages with the toggle.
Use a manual fixed-size last-seen table so the window updates are explicit without built-in map classes.
Complexity: Time O(n), Space O(min(n, alphabet))
Longest unique substring with variable window
function longestUniqueWindow(text: string): number {
const lastSeenIndexes = new Array<number>(256).fill(-1)
let leftIndex = 0
let bestLength = 0
for (let rightIndex = 0; rightIndex < text.length; rightIndex += 1) {
const charCode = text.charCodeAt(rightIndex) & 255
const seenIndex = lastSeenIndexes[charCode]
if (seenIndex >= leftIndex) {
leftIndex = seenIndex + 1
}
lastSeenIndexes[charCode] = rightIndex
const currentWindowLength = rightIndex - leftIndex + 1
if (currentWindowLength > bestLength) {
bestLength = currentWindowLength
}
}
return bestLength
}
Watch Out
The bugs and edge cases that bite engineers most often. Skim before you ship.
Pro Tips
Small habits that pay off every time you reach for this pattern.
Pattern Recognition
Concrete signals that tell you this is the right pattern both at work and on LeetCode.
Practice
A curated easy-to-hard problem ladder. Each one reinforces a specific aspect of the pattern.
| # | Problem | Difficulty | Typical Complexity |
|---|---|---|---|
| 1 | Maximum Average Subarray I | Easy | O(n) |
| 2 | Minimum Size Subarray Sum | Medium | O(n) |
| 3 | Longest Substring Without Repeating Characters | Medium | O(n) |
| 4 | Permutation in String | Medium | O(n) |
| 5 | Find All Anagrams in a String | Medium | O(n) |
| 6 | Longest Repeating Character Replacement | Medium | O(n) |
| 7 | Subarray Product Less Than K | Medium | O(n) |
| 8 | Minimum Window Substring | Hard | O(n) |
| 9 | Sliding Window Maximum | Hard | O(n) |
| 10 | Substring with Concatenation of All Words | Hard | O(n*k) |