← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Sliding Window

Algorithms Intermediate
debt(d7/e3/b3/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints state automated=no and the code_pattern is a nested loop over sequential data. Blackfire (the listed tool) is a profiler that can surface O(n²) performance regressions at runtime, but it won't flag 'this should be a sliding window' statically — a developer must recognise the pattern through code review or notice the slowdown under load. No static linter rule catches this automatically.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix describes replacing a brute-force nested loop with the two-pointer expand/shrink pattern. This is more than a one-line swap (you must restructure the loop logic, maintain running state, and handle boundary conditions) but is typically contained within a single function or component — not a cross-cutting refactor.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The choice to use or misuse sliding window is scoped to individual algorithmic functions handling arrays or strings. The applies_to covers web and cli contexts broadly, but the pattern itself is self-contained — a wrong implementation in one function doesn't impose structural weight on the rest of the codebase.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field identifies that developers wrongly believe sliding window only applies to arrays, missing strings and other sequential structures. The common_mistakes add further documented gotchas: fixed vs variable window confusion, forgetting to update state on left-pointer removal, and off-by-one errors. These are well-known but non-obvious pitfalls that most developers encounter and eventually learn.

About DEBT scoring →

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;
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 49
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 4 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 3 pings S 0 pings M 1 ping T 0 pings W
No pings yet today
SEMrush 1
Amazonbot 10 Ahrefs 4 Perplexity 4 Google 3 Bing 3 Scrapy 3 PetalBot 3 Unknown AI 2 Claude 2 ChatGPT 2 Meta AI 1 Sogou 1 SEMrush 1
crawler 34 crawler_json 5
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


✓ schema.org compliant