{
    "slug": "sliding_window",
    "term": "Sliding Window",
    "category": "algorithms",
    "difficulty": "intermediate",
    "short": "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).",
    "long": "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.",
    "aliases": [
        "sliding window algorithm",
        "window technique"
    ],
    "tags": [
        "algorithms",
        "arrays",
        "strings",
        "optimisation"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "two_pointer_technique",
        "big_o_notation",
        "time_complexity"
    ],
    "prerequisites": [
        "big_o_notation",
        "array_data_structure",
        "string_algorithms"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Sliding_window_protocol"
    ],
    "bad_code": "// O(n²) — recompute sum from scratch for each window:\nfunction maxSumSubarray(array $arr, int $k): int {\n    $max = 0;\n    for ($i = 0; $i <= count($arr) - $k; $i++) {\n        $sum = array_sum(array_slice($arr, $i, $k)); // O(k) per position\n        $max = max($max, $sum);\n    }\n    return $max;\n}",
    "good_code": "// O(n) — maintain running window sum:\nfunction maxSumSubarray(array $arr, int $k): int {\n    $windowSum = array_sum(array_slice($arr, 0, $k));\n    $max = $windowSum;\n    for ($i = $k; $i < count($arr); $i++) {\n        $windowSum += $arr[$i] - $arr[$i - $k]; // Slide: add new, remove old\n        $max = max($max, $windowSum);\n    }\n    return $max;\n}",
    "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",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/sliding_window",
        "html_url": "https://codeclaritylab.com/glossary/sliding_window",
        "json_url": "https://codeclaritylab.com/glossary/sliding_window.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Sliding Window](https://codeclaritylab.com/glossary/sliding_window) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/sliding_window"
            }
        }
    }
}