TheAlgorithms/Python · error · ValueError

find_max_recursive() arg is an empty sequence

Error message

find_max_recursive() arg is an empty sequence

What it means

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.

Source

Thrown at maths/find_max.py:62

    >>> find_max_recursive(nums, 0, len(nums) - 1) == max(nums)
    True
    >>> find_max_recursive([], 0, 0)
    Traceback (most recent call last):
        ...
    ValueError: find_max_recursive() arg is an empty sequence
    >>> find_max_recursive(nums, 0, len(nums)) == max(nums)
    Traceback (most recent call last):
        ...
    IndexError: list index out of range
    >>> find_max_recursive(nums, -len(nums), -1) == max(nums)
    True
    >>> find_max_recursive(nums, -len(nums) - 1, -1) == max(nums)
    Traceback (most recent call last):
        ...
    IndexError: list index out of range
    """
    if len(nums) == 0:
        raise ValueError("find_max_recursive() arg is an empty sequence")
    if (
        left >= len(nums)
        or left < -len(nums)
        or right >= len(nums)
        or right < -len(nums)
    ):
        raise IndexError("list index out of range")
    if left == right:
        return nums[left]
    mid = (left + right) >> 1  # the middle
    left_max = find_max_recursive(nums, left, mid)  # find max in range[left, mid]
    right_max = find_max_recursive(
        nums, mid + 1, right
    )  # find max in range[mid + 1, right]

    return left_max if left_max >= right_max else right_max

View on GitHub (pinned to f5988cc097)

Solutions

  1. Guard the call site: `if not nums: return default` or raise your own domain-specific error.
  2. Validate emptiness at the data-ingestion boundary.
  3. If you do not need divide-and-conquer, use find_max_iterative or built-in max, which have the same empty-input contract.

Example fix

# before
m = find_max_recursive(values, 0, len(values) - 1)  # values may be []

# after
if not values:
    raise ValueError('cannot compute max of empty dataset')
m = find_max_recursive(values, 0, len(values) - 1)
Defensive patterns

Strategy: validation

Validate before calling

if not nums:
    raise ValueError('cannot compute max of empty dataset')
m = find_max_recursive(nums, 0, len(nums) - 1)

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/ce4caef5eb2f1f2d. Report an issue: GitHub.