{"record":{"id":"12ce451f4da2abe8","repo":"TheAlgorithms/Python","slug":"the-array-is-empty","errorCode":null,"errorMessage":"The array is empty.","messagePattern":"The array is empty\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/arrays/prefix_sum.py","lineNumber":51,"sourceCode":"        >>> PrefixSum([]).get_sum(0, 0)\n        Traceback (most recent call last):\n        ...\n        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)","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/arrays/prefix_sum.py#L33-L69","documentation":"Raised by PrefixSum.get_sum() in data_structures/arrays/prefix_sum.py when the underlying prefix_sum list is empty — i.e. the PrefixSum was built (directly or effectively) from an empty array. The emptiness check runs before the range check, so any get_sum call on an empty structure raises this first.","triggerScenarios":"Calling PrefixSum([]).get_sum(0, 0), or calling get_sum before any data was loaded into the structure. Empty arrays passed to the constructor produce an empty prefix_sum list.","commonSituations":"Querying a prefix-sum structure that was initialized but not yet populated, datasets that load asynchronously where a query races ahead of the data load, or empty query windows in data pipelines.","solutions":["Check the source array is non-empty before constructing/querying: if data: ps = PrefixSum(data).","Ensure queries only run after data population completes (ordering, readiness flag).","Return a sentinel (0 or None) for empty data instead of calling get_sum."],"exampleFix":"# before\nps = PrefixSum([])\ntotal = ps.get_sum(0, 0)  # ValueError\n\n# after\ntotal = 0  # defined result for empty range\ndata = [x for x in source if keep(x)]\nif data:\n    total = PrefixSum(data).get_sum(0, len(data) - 1)","handlingStrategy":"validation","validationCode":"if not data:\n    return 0  # no data, no range sums\nps = PrefixSum(data)\ntotal = ps.get_sum(0, len(data) - 1)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only construct/query PrefixSum after data is populated","Check emptiness once at construction, not per query"],"tags":["value-validation","prefix-sum","range-query","empty-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}