{"record":{"id":"84d37d89ad8f30d5","repo":"TheAlgorithms/Python","slug":"invalid-range-specified","errorCode":null,"errorMessage":"Invalid range specified.","messagePattern":"Invalid range specified\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/arrays/prefix_sum.py","lineNumber":54,"sourceCode":"        ValueError: The array is empty.\n        >>> PrefixSum([1,2,3]).get_sum(-1, 2)\n        Traceback (most recent call last):\n        ...\n        ValueError: Invalid range specified.\n        >>> PrefixSum([1,2,3]).get_sum(2, 3)\n        Traceback (most recent call last):\n        ...\n        ValueError: Invalid range specified.\n        >>> PrefixSum([1,2,3]).get_sum(2, 1)\n        Traceback (most recent call last):\n        ...\n        ValueError: Invalid range specified.\n        \"\"\"\n        if not self.prefix_sum:\n            raise ValueError(\"The array is empty.\")\n\n        if start < 0 or end >= len(self.prefix_sum) or start > end:\n            raise ValueError(\"Invalid range specified.\")\n\n        if start == 0:\n            return self.prefix_sum[end]\n\n        return self.prefix_sum[end] - self.prefix_sum[start - 1]\n\n    def contains_sum(self, target_sum: int) -> bool:\n        \"\"\"\n        The function returns True if array contains the target_sum,\n        False otherwise.\n\n        Runtime : O(n)\n        Space: O(n)\n\n        >>> PrefixSum([1,2,3]).contains_sum(6)\n        True\n        >>> PrefixSum([1,2,3]).contains_sum(5)\n        True","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/arrays/prefix_sum.py#L36-L72","documentation":"Raised by PrefixSum.get_sum() in data_structures/arrays/prefix_sum.py when the requested [start, end] range is invalid for the populated array: start < 0, end >= len(prefix_sum), or start > end. Valid ranges are inclusive on both ends within array bounds.","triggerScenarios":"Calling get_sum(2, 3) or get_sum(2, 1) on PrefixSum([1,2,3]) (end 3 out of bounds; start > end), get_sum(-1, 2), or get_sum(0, n) where n == len(arr) — the classic inclusive-end off-by-one.","commonSituations":"Treating end as exclusive when it is inclusive (passing len(arr) instead of len(arr)-1), reversed bounds from swapped arguments, or ranges computed from user input without bound checks.","solutions":["Remember both bounds are inclusive: the last valid end is len(arr) - 1, not len(arr).","Normalize arguments before calling: if start > end: start, end = end, start.","Validate: 0 <= start <= end < len(arr) before invoking get_sum."],"exampleFix":"# before\nps.get_sum(0, len(arr))  # end one past the last index\n\n# after\nps.get_sum(0, len(arr) - 1)  # inclusive end","handlingStrategy":"validation","validationCode":"n = len(arr)\nstart, end = max(0, start), min(end, n - 1)\nif start > end:\n    raise ValueError('empty normalized range')\ntotal = ps.get_sum(start, end)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Both bounds are inclusive: last valid end is len(arr) - 1","Swap swapped bounds before calling","Validate 0 <= start <= end < len(arr)"],"tags":["value-validation","prefix-sum","range-query","off-by-one"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}