{"record":{"id":"625ce6aff586ab9c","repo":"TheAlgorithms/Python","slug":"invalid-upper-or-lower-bound","errorCode":null,"errorMessage":"Invalid upper or lower bound!","messagePattern":"Invalid upper or lower bound!","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"searches/linear_search.py","lineNumber":58,"sourceCode":"    :param sequence: a collection with comparable items (as sorted items not required\n        in Linear Search)\n    :param low: Lower bound of the array\n    :param high: Higher bound of the array\n    :param target: The element to be found\n    :return: Index of the key or -1 if key not found\n\n    Examples:\n    >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0)\n    0\n    >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700)\n    4\n    >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30)\n    1\n    >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6)\n    -1\n    \"\"\"\n    if not (0 <= high < len(sequence) and 0 <= low < len(sequence)):\n        raise Exception(\"Invalid upper or lower bound!\")\n    if high < low:\n        return -1\n    if sequence[low] == target:\n        return low\n    if sequence[high] == target:\n        return high\n    return rec_linear_search(sequence, low + 1, high - 1, target)\n\n\nif __name__ == \"__main__\":\n    user_input = input(\"Enter numbers separated by comma:\\n\").strip()\n    sequence = [int(item.strip()) for item in user_input.split(\",\")]\n\n    target = int(input(\"Enter a single number to be found in the list:\\n\").strip())\n    result = linear_search(sequence, target)\n    if result != -1:\n        print(f\"linear_search({sequence}, {target}) = {result}\")\n    else:","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/searches/linear_search.py#L40-L76","documentation":"Raised by rec_linear_search in searches/linear_search.py when either the low or high index argument is outside the sequence bounds. The function searches inward from both ends simultaneously, so it demands 0 <= high < len(sequence) and 0 <= low < len(sequence); any out-of-range index raises a bare Exception (not ValueError, which is itself a code smell). The classic trigger is passing len(sequence) as high (off-by-one) instead of len(sequence) - 1.","triggerScenarios":"rec_linear_search([0, 30, 500], 0, 0, 3) -> high == len -> raises; negative low like rec_linear_search(seq, x, -1, 4); calling with defaults omitted. The doctest-style API expects rec_linear_search(sequence, low, high, target), so swapping low/high also produces it when high lands out of range.","commonSituations":"Off-by-one errors from using len(seq) instead of len(seq) - 1; porting code from ranges where the end bound is exclusive; passing bounds from enumerate() or slice ends directly.","solutions":["Pass high = len(sequence) - 1 and low = 0 for a full search: rec_linear_search(seq, 0, len(seq) - 1, target).","Validate bounds before the call: assert 0 <= low <= high < len(sequence).","Catch it knowing it is a plain Exception (except Exception), or better, fix the caller so no exception occurs; consider contributing a patch to raise ValueError instead."],"exampleFix":"# before\nrec_linear_search(seq, 0, len(seq), target)  # high out of range\n\n# after\nrec_linear_search(seq, 0, len(seq) - 1, target)","handlingStrategy":"validation","validationCode":"def safe_rec_linear_search(seq, target, low=0, high=None):\n    if high is None:\n        high = len(seq) - 1\n    if not (0 <= low < len(seq) and 0 <= high < len(seq)):\n        raise IndexError('low/high must be within [0, len(sequence))')\n    return rec_linear_search(seq, low, high, target)","typeGuard":null,"tryCatchPattern":"try:\n    i = rec_linear_search(seq, low, high, target)\nexcept Exception as e:  # note: bare Exception, not ValueError\n    if 'Invalid upper or lower bound' in str(e):\n        i = rec_linear_search(seq, 0, len(seq) - 1, target)\n    else:\n        raise","preventionTips":["high is inclusive here: pass len(seq) - 1, not len(seq).","Argument order is (sequence, low, high, target) — swapping low/high silently breaks bounds.","Because the function raises a generic Exception, validate bounds yourself instead of catching."],"tags":["search","linear-search","recursion","off-by-one","bounds"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}