{"record":{"id":"ce4caef5eb2f1f2d","repo":"TheAlgorithms/Python","slug":"find-max-recursive-arg-is-an-empty-sequence","errorCode":null,"errorMessage":"find_max_recursive() arg is an empty sequence","messagePattern":"find_max_recursive\\(\\) arg is an empty sequence","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/find_max.py","lineNumber":62,"sourceCode":"    >>> find_max_recursive(nums, 0, len(nums) - 1) == max(nums)\n    True\n    >>> find_max_recursive([], 0, 0)\n    Traceback (most recent call last):\n        ...\n    ValueError: find_max_recursive() arg is an empty sequence\n    >>> find_max_recursive(nums, 0, len(nums)) == max(nums)\n    Traceback (most recent call last):\n        ...\n    IndexError: list index out of range\n    >>> find_max_recursive(nums, -len(nums), -1) == max(nums)\n    True\n    >>> find_max_recursive(nums, -len(nums) - 1, -1) == max(nums)\n    Traceback (most recent call last):\n        ...\n    IndexError: list index out of range\n    \"\"\"\n    if len(nums) == 0:\n        raise ValueError(\"find_max_recursive() arg is an empty sequence\")\n    if (\n        left >= len(nums)\n        or left < -len(nums)\n        or right >= len(nums)\n        or right < -len(nums)\n    ):\n        raise IndexError(\"list index out of range\")\n    if left == right:\n        return nums[left]\n    mid = (left + right) >> 1  # the middle\n    left_max = find_max_recursive(nums, left, mid)  # find max in range[left, mid]\n    right_max = find_max_recursive(\n        nums, mid + 1, right\n    )  # find max in range[mid + 1, right]\n\n    return left_max if left_max >= right_max else right_max\n\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/find_max.py#L44-L80","documentation":"Raised by find_max_recursive() in maths/find_max.py when nums is an empty list. This divide-and-conquer maximum recursively splits on indices and terminates at nums[left]; with no elements there is nothing to return, so it raises ValueError before index validation.","triggerScenarios":"Calling find_max_recursive([], left, right) with any bounds. The `if len(nums) == 0` check fires first, before the IndexError bounds checks. Passing a non-empty list with bad bounds raises IndexError instead (see error 556).","commonSituations":"Empty filtered results or empty batches fed into the recursive helper; wrappers that pass user data straight through without size checks; unit tests exercising empty-input paths.","solutions":["Guard the call site: `if not nums: return default` or raise your own domain-specific error.","Validate emptiness at the data-ingestion boundary.","If you do not need divide-and-conquer, use find_max_iterative or built-in max, which have the same empty-input contract."],"exampleFix":"# before\nm = find_max_recursive(values, 0, len(values) - 1)  # values may be []\n\n# after\nif not values:\n    raise ValueError('cannot compute max of empty dataset')\nm = find_max_recursive(values, 0, len(values) - 1)","handlingStrategy":"validation","validationCode":"if not nums:\n    raise ValueError('cannot compute max of empty dataset')\nm = find_max_recursive(nums, 0, len(nums) - 1)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check emptiness before computing bounds.","Pass inclusive bounds (0, len(nums) - 1) — see also error 556.","Keep empty-handling at one layer instead of scattering it around recursive helpers."],"tags":["math","max","recursion","empty-sequence","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}