{"record":{"id":"a517f7cba7cf2f99","repo":"geekcomputers/Python","slug":"invalid-position-a517f7","errorCode":null,"errorMessage":"Invalid Position","messagePattern":"Invalid Position","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LinkedLists all Types/singly_linked_list.py","lineNumber":92,"sourceCode":"        while temp:\n            print(f\"{temp.data} ->\", end=\" \")\n            temp = temp.next\n        print(\"NULL\")\n\n    def len(self):\n        return self.length  # O(1) length calculation\n        # if self.head is None:\n        #     return 0\n        # count = 0\n        # temp = self.head\n        # while temp:\n        #     count += 1\n        #     temp = temp.next\n        # return count\n\n    def remove_at(self, idx):\n        if idx < 0 or self.len() <= idx:\n            raise Exception(\"Invalid Position\")\n        if idx == 0:\n            self.head = self.head.next\n            self.length -= 1\n            return\n        temp = self.head\n        dist = 0\n        while dist != idx - 1:\n            dist += 1\n            temp = temp.next\n        temp.next = temp.next.next\n        self.length -= 1\n\n    def insert_at(self, idx: int, data):\n        if idx < 0 or self.len() < idx:\n            raise Exception(\"Invalid Position\")\n        if idx == 0:\n            self.insert_front(data)\n            return","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/geekcomputers/Python/blob/40f4cd2652d75ef8e49d76e5c4d431d458712719/LinkedLists all Types/singly_linked_list.py#L74-L110","documentation":"Raised by remove_at on a singly linked list when idx is negative or idx >= length (condition `idx < 0 or self.len() <= idx`). The list cannot remove a node that does not exist, so out-of-range indices are rejected before traversal begins.","triggerScenarios":"Calling sll.remove_at(idx) with idx < 0 or idx >= sll.len(), e.g. remove_at(len()) to pop the last element (the last valid index is len()-1).","commonSituations":"Pop-from-end style code assuming remove_at(len()) removes the tail; loop counters off by one; stale indices after other removals in the same loop.","solutions":["Use 0 <= idx < sll.len() (remember the max valid index is len()-1)","To remove the last node, call remove_at(sll.len() - 1)","When removing while iterating, adjust the index after each removal or iterate backwards","Track the length once and re-validate after mutations"],"exampleFix":"// before\nsll.remove_at(sll.len())  # Exception: Invalid Position\n\n// after\nif sll.len() > 0:\n    sll.remove_at(sll.len() - 1)  # removes tail","handlingStrategy":"validation","validationCode":"if 0 <= idx < sll.len():\n    sll.remove_at(idx)\n# to pop tail:\nif sll.len():\n    sll.remove_at(sll.len() - 1)","typeGuard":"def is_valid_remove_idx(sll, idx) -> bool:\n    return isinstance(idx, int) and 0 <= idx < sll.len()","tryCatchPattern":"try:\n    sll.remove_at(idx)\nexcept Exception as e:\n    if str(e) == 'Invalid Position':\n        pass  # already removed / nothing there\n    else:\n        raise","preventionTips":["Max valid index is len()-1, not len()","When removing during iteration, decrement your cursor or iterate backwards","Cache length only if no concurrent mutations occur"],"tags":["singly-linked-list","remove","index-out-of-range"],"backgroundTag":"index-out-of-bounds","analyzedSha":"40f4cd2652d75ef8e49d76e5c4d431d458712719","analyzedAt":"2026-08-27T11:12:20.313Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}