{"record":{"id":"ac70e8c50a1615ff","repo":"geekcomputers/Python","slug":"invalid-position-ac70e8","errorCode":null,"errorMessage":"Invalid Position","messagePattern":"Invalid Position","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LinkedLists all Types/doubly_linked_list.py","lineNumber":95,"sourceCode":"                break\r\n            print(f\"{temp.data} <=>\", end=\" \")\r\n            temp = temp.next\r\n        print(\"NULL\")\r\n\r\n    def len(self):\r\n        return self.length  # O(1) length calculation\r\n        # if self.head is None:\r\n        #     return 0\r\n        # count = 0\r\n        # temp = self.head\r\n        # while temp:\r\n        #     count += 1\r\n        #     temp = temp.next\r\n        # return count\r\n\r\n    def remove_at(self, idx):\r\n        if idx < 0 or self.len() <= idx:\r\n            raise Exception(\"Invalid Position\")\r\n        if idx == 0:\r\n            self.pop_front()\r\n            return\r\n        elif idx == self.length - 1:\r\n            self.pop_back()\r\n            return\r\n        temp = self.head\r\n        dist = 0\r\n        while dist != idx - 1:\r\n            dist += 1\r\n            temp = temp.next\r\n        temp.next = temp.next.next\r\n        temp.next.prev = temp.next.prev.prev\r\n        self.length -= 1\r\n\r\n    def insert_at(self, idx: int, data):\r\n        if idx < 0 or self.len() < idx:\r\n            raise Exception(\"Invalid Position\")\r","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/geekcomputers/Python/blob/40f4cd2652d75ef8e49d76e5c4d431d458712719/LinkedLists all Types/doubly_linked_list.py#L77-L113","documentation":"remove_at in the doubly linked list raises a generic Exception when idx < 0 or idx >= len() (the code writes self.len() <= idx). Indices 0 and length-1 delegate to pop_front/pop_back after validation; everything else unlinks the node in place.","triggerScenarios":"Calling remove_at(idx) with a negative index, an index equal to the list length, or on an empty list; using a stale index after the list shrank between lookup and removal.","commonSituations":"Index computed from find returning -1 on miss; concurrent modification patterns where the list changed between measuring and removing; 1-based vs 0-based confusion after porting pseudocode.","solutions":["Check 0 <= idx < dll.len() before calling remove_at","Handle 'not found' (-1) results separately instead of passing them to remove_at","In removal loops, account for the list shrinking each iteration"],"exampleFix":"# before\ndll.remove_at(idx)\n# after\nif 0 <= idx < dll.len():\n    dll.remove_at(idx)\nelse:\n    raise IndexError('remove_at index out of range')","handlingStrategy":"validation","validationCode":"def can_remove(dll, idx) -> bool:\n    return 0 <= idx < dll.len()","typeGuard":null,"tryCatchPattern":"try:\n    dll.remove_at(idx)\nexcept Exception:\n    raise IndexError(f'remove index {idx} out of range') from None","preventionTips":["Re-check len() right before removal, not from a stale cached value","Handle -1 'not found' results separately","Adjust indices when removing multiple elements in a loop"],"tags":["linked-list","doubly-linked-list","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"}