{"record":{"id":"33cd3c4f199c0aa3","repo":"TheAlgorithms/Python","slug":"window-size-must-be-a-positive-integer-33cd3c","errorCode":null,"errorMessage":"Window size must be a positive integer","messagePattern":"Window size must be a positive integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"other/sliding_window_maximum.py","lineNumber":36,"sourceCode":"        ValueError: If window_size is not a positive integer.\n\n    Time Complexity: O(n) - each element is added and removed at most once\n    Space Complexity: O(k) - deque stores at most window_size indices\n\n    Examples:\n    >>> sliding_window_maximum([1, 3, -1, -3, 5, 3, 6, 7], 3)\n    [3, 3, 5, 5, 6, 7]\n    >>> sliding_window_maximum([9, 11], 2)\n    [11]\n    >>> sliding_window_maximum([], 3)\n    []\n    >>> sliding_window_maximum([4, 2, 12, 3], 1)\n    [4, 2, 12, 3]\n    >>> sliding_window_maximum([1], 1)\n    [1]\n    \"\"\"\n    if window_size <= 0:\n        raise ValueError(\"Window size must be a positive integer\")\n    if not numbers:\n        return []\n\n    result: list[int] = []\n    index_deque: deque[int] = deque()\n\n    for current_index, current_value in enumerate(numbers):\n        # Remove the element which is out of this window\n        if index_deque and index_deque[0] == current_index - window_size:\n            index_deque.popleft()\n\n        # Remove useless elements (smaller than current) from back\n        while index_deque and numbers[index_deque[-1]] < current_value:\n            index_deque.pop()\n\n        index_deque.append(current_index)\n\n        # Start adding to result once we have a full window","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/other/sliding_window_maximum.py#L18-L54","documentation":"Raised by sliding_window_maximum when window_size is <= 0. The deque-based algorithm assumes each window covers at least one element, so zero or negative window sizes are rejected up front. An empty numbers list is legal and returns [] — only the window size is validated.","triggerScenarios":"Calling sliding_window_maximum([1,2,3], 0), sliding_window_maximum([1,2,3], -2), or passing a window size computed from an expression that can reach 0.","commonSituations":"Window size derived from user configuration or from a formula like len(numbers) - k that can hit zero or below on short inputs.","solutions":["Pass a positive window size (>= 1); window_size=1 returns the input unchanged","Clamp computed sizes: window_size = max(1, window_size)","Validate config before the call and raise a clear upstream error if an invalid size is configured"],"exampleFix":"# before\nresult = sliding_window_maximum(nums, len(nums) - k)  # k >= len(nums) -> 0 or negative\n\n# after\nresult = sliding_window_maximum(nums, max(1, len(nums) - k))","handlingStrategy":"validation","validationCode":"def valid_window_size(window_size) -> bool:\n    return isinstance(window_size, int) and window_size >= 1","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp computed window sizes with max(1, k)","Validate window parameters from config against positive integers at load time"],"tags":["sliding-window","argument-validation","range-check"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}