{"record":{"id":"71c8f746b730b02f","repo":"TheAlgorithms/Python","slug":"invalid-value-of-position","errorCode":null,"errorMessage":"Invalid value of 'position'","messagePattern":"Invalid value of 'position'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/arrays/kth_largest_element.py","lineNumber":99,"sourceCode":"        >>> kth_largest_element([-2, -5, -4, -1], 1)\n        -1\n        >>> kth_largest_element([], 1)\n        -1\n        >>> kth_largest_element([3.1, 1.2, 5.6, 4.7, 7.9, 5, 0], 1.5)\n        Traceback (most recent call last):\n        ...\n        ValueError: The position should be an integer\n        >>> kth_largest_element((4, 6, 1, 2), 4)\n        Traceback (most recent call last):\n        ...\n        TypeError: 'tuple' object does not support item assignment\n    \"\"\"\n    if not arr:\n        return -1\n    if not isinstance(position, int):\n        raise ValueError(\"The position should be an integer\")\n    if not 1 <= position <= len(arr):\n        raise ValueError(\"Invalid value of 'position'\")\n    low, high = 0, len(arr) - 1\n    while low <= high:\n        if low > len(arr) - 1 or high < 0:\n            return -1\n        pivot_index = partition(arr, low, high)\n        if pivot_index == position - 1:\n            return arr[pivot_index]\n        elif pivot_index > position - 1:\n            high = pivot_index - 1\n        else:\n            low = pivot_index + 1\n    return -1\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/arrays/kth_largest_element.py#L81-L117","documentation":"Raised by kth_largest_element() in data_structures/arrays/kth_largest_element.py when position is an int but outside 1..len(arr) inclusive. position=1 means the largest element and position=len(arr) the smallest; anything else (0, negative, or greater than the length) is rejected.","triggerScenarios":"Calling kth_largest_element([3,1,5,4,7,5,0], 0), kth_largest_element([3,1,5], 4), or any position > len(arr).","commonSituations":"Confusing 0-based and 1-based semantics (asking for 'position 0' expecting the max), k larger than the dataset size in top-k queries, or k computed as len(arr) + 1 by an off-by-one loop.","solutions":["Clamp or reject k at the call site: if not 1 <= k <= len(arr): handle.","Remember this API is 1-based: k=1 is the largest, k=len(arr) the smallest.","For top-k features, check k against the current data size before every call (data may have shrunk)."],"exampleFix":"# before\nbest = kth_largest_element(scores, 0)  # expecting max\n\n# after\nbest = kth_largest_element(scores, 1)  # max, 1-based","handlingStrategy":"validation","validationCode":"if not 1 <= k <= len(arr):\n    raise ValueError(f'k must be in 1..{len(arr)}')\nresult = kth_largest_element(arr, k)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["k is 1-based: 1 = largest, len(arr) = smallest","Re-check k against current data size before each call"],"tags":["value-validation","array","selection","quickselect","off-by-one"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}