Sliding Window
Also Known As
sliding window algorithm
window technique
TL;DR
An algorithmic technique that maintains a window of elements over a sequence, expanding or contracting it to find subarrays or substrings satisfying a condition in O(n).
Explanation
Instead of recomputing a result from scratch for each position (O(n²) or worse), the sliding window maintains running state — adding the new element entering the window and removing the one leaving. Fixed-size windows compute moving averages or max sums. Variable-size windows find the shortest/longest subarray meeting a constraint. Common uses: maximum sum subarray, longest substring without repeating characters, minimum window substring.
Diagram
flowchart LR
ARR[Array: 1 3 5 2 4 6 1<br/>Find max sum of 3 elements]
subgraph Naive_O_n_k
N1[Check 1,3,5 = 9]
N2[Check 3,5,2 = 10]
N3[Check 5,2,4 = 11]
N1 --> N2 --> N3
end
subgraph SlidingWindow_O_n
W1[Window 1,3,5 = 9]
W2[Slide: remove 1 add 2<br/>9-1+2 = 10]
W3[Slide: remove 3 add 4<br/>10-3+4 = 11]
W1 --> W2 --> W3
end
RESULT[Max sum = 11]
style W1 fill:#1f6feb,color:#fff
style W3 fill:#238636,color:#fff
style RESULT fill:#238636,color:#fff
Common Misconception
✗ Sliding window only works on arrays — it applies equally to strings and any sequential data structure.
Why It Matters
Transforms O(n²) or O(n³) string/array problems into O(n), critical for log parsing, stream processing, and substring search.
Common Mistakes
- Not expanding/shrinking the window based on the constraint — moving a fixed step instead.
- Forgetting to update the running state when removing the leftmost element.
- Using a fixed-size window when a variable-size window is needed (or vice versa).
- Off-by-one errors in window boundary calculations.
Code Examples
✗ Vulnerable
// O(n²) — recompute sum from scratch for each window:
function maxSumSubarray(array $arr, int $k): int {
$max = 0;
for ($i = 0; $i <= count($arr) - $k; $i++) {
$sum = array_sum(array_slice($arr, $i, $k)); // O(k) per position
$max = max($max, $sum);
}
return $max;
}
✓ Fixed
// O(n) — maintain running window sum:
function maxSumSubarray(array $arr, int $k): int {
$windowSum = array_sum(array_slice($arr, 0, $k));
$max = $windowSum;
for ($i = $k; $i < count($arr); $i++) {
$windowSum += $arr[$i] - $arr[$i - $k]; // Slide: add new, remove old
$max = max($max, $windowSum);
}
return $max;
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
24
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Amazonbot 1
Amazonbot 10
Perplexity 4
Ahrefs 2
Unknown AI 2
Google 2
Also referenced
How they use it
crawler 19
crawler_json 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Medium
⚡ Quick Fix
Use sliding window when you need to track a subset of a sequential data structure — expand right pointer, shrink left pointer, maintaining a constraint; O(n) vs O(n²) brute force
📦 Applies To
any
web
cli
🔗 Prerequisites
🔍 Detection Hints
Nested loop over sequential data looking for subarray with property; O(n²) substring search that sliding window would make O(n)
Auto-detectable:
✗ No
blackfire
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Medium
Context: Function
Tests: Update