Sliding Window Technique
Channel: Profound Academy
Full video link:
https://www.youtube.com/watch?v=dOonV4byDEgAlgorithm • 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) |
Prefer video learning? This explainer gives a quick visual walkthrough of the core idea before you dive into the detailed sections below.
Sliding Window Technique
Channel: Profound Academy
Full video link:
https://www.youtube.com/watch?v=dOonV4byDEgSee how a moving window expands and shrinks while preserving constraints.
Expand the window to include new elements.
Input
A B C A B E B E
Window
[A B C]
Left / Right
0 / 2
Constraint
No repeated characters.
Learn the core building blocks and terminology in one place before comparisons, so the mechanics are clear and duplicates are removed.
Window boundaries
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.
Add step
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.
Shrink step
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.
State tracker
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.
Best answer update
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.
Fixed window
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.
Variable window
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.
Window validity
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.
Amortized linear
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.
Frequency map
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.
This walkthrough connects the core concepts of Sliding Window into one end-to-end execution flow.
Step 1
Left and right pointers defining active range.
Before
window=
After
Transition
Why this step matters: Window boundaries is a required building block for understanding how Sliding Window stays correct and performant on large inputs.
Step 2
Include right element and update state.
Before
After
Transition
Why this step matters: Add step is a required building block for understanding how Sliding Window stays correct and performant on large inputs.
Step 3
Move left boundary until constraints restore.
Before
After
Transition
Why this step matters: Shrink step is a required building block for understanding how Sliding Window stays correct and performant on large inputs.
Step 4
Counts/sums/sets needed for validity checks.
Before
After
Transition
Why this step matters: State tracker is a required building block for understanding how Sliding Window stays correct and performant on large inputs.
vs Prefix sums
When to choose this: Choose sliding window when constraints depend on mutable content inside range.
Tradeoff: Prefix sums are better for static sum queries.
vs Two pointers
When to choose this: Choose sliding window for contiguous range optimization.
Tradeoff: General two pointers may be simpler for pairwise non-window problems.
vs Brute-force subarray scan
When to choose this: Choose sliding window to avoid O(n^2) range enumeration.
Tradeoff: Needs careful state update design.
Datadog
Monitoring alerts evaluate rolling windows over metrics streams to detect spikes and anomalies.
Takeaway: Sliding windows are practical for continuous observability workloads.
Cloudflare
Rate-limiting systems apply per-key request windows to enforce fair usage.
Takeaway: Window-based constraints protect services under burst traffic.
Spotify
Session analytics often rely on rolling windows for engagement and retention signals.
Takeaway: Window logic underpins many product analytics metrics.
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
}
Use these signals to decide if this data structure/algorithm is the right fit before implementation.
| # | 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) |