{"record":{"id":"f64a4a186774271c","repo":"geekcomputers/Python","slug":"invalid-position","errorCode":null,"errorMessage":"Invalid Position","messagePattern":"Invalid Position","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LinkedLists all Types/circular_linked_list.py","lineNumber":87,"sourceCode":"        if self.head is None:\r\n            print(\"The List is Empty!\")\r\n            return\r\n        temp = self.head.next\r\n        print(f\"{self.head.data} ->\", end=\" \")\r\n        while temp != self.head:\r\n            print(f\"{temp.data} ->\", end=\" \")\r\n            temp = temp.next\r\n        print(f\"{self.tail.next.data}\")\r\n\r\n    def insert_at(self, idx, data):\r\n        if idx == 0:\r\n            self.insert_at_beginning(data)\r\n            return\r\n        elif idx == self.length:\r\n            self.insert_at_end(data)\r\n            return\r\n        elif 0 > idx or idx > self.length:\r\n            raise Exception(\"Invalid Position\")\r\n            return\r\n        pos = 0\r\n        temp = self.head\r\n        while temp:\r\n            if pos == idx - 1:\r\n                node = Node(data, temp.next)\r\n                temp.next = node\r\n                self.length += 1\r\n                return\r\n            pos += 1\r\n            temp = temp.next\r\n\r\n    def remove_at(self, idx):\r\n        if 0 > idx or idx >= self.length:\r\n            raise Exception(\"Invalid Position\")\r\n        elif idx == 0:\r\n            self.pop_at_beginning()\r\n            return\r","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/geekcomputers/Python/blob/40f4cd2652d75ef8e49d76e5c4d431d458712719/LinkedLists all Types/circular_linked_list.py#L69-L105","documentation":"insert_at in the circular linked list raises a generic Exception when idx is negative or greater than the list length, i.e. the insertion position is outside the valid range [0, length]. Bounds 0 and length are handled by insert_at_beginning/insert_at_end before this check.","triggerScenarios":"Calling insert_at(idx) with idx < 0 or idx > list.length, e.g. insert_at(10) on a 3-node list; computing an index from user input or search results that returned -1/not-found.","commonSituations":"Using find()/index-of returning -1 as an insert position; off-by-one when iterating positions; inserting into an empty list with idx > 0.","solutions":["Validate idx against 0 <= idx <= len(list)/.length before calling insert_at","Treat -1 from search as 'not found' and skip the insert instead of passing it through","Prefer append/insert_at_end when adding at the tail"],"exampleFix":"# before\ncll.insert_at(pos, data)\n# after\nif 0 <= pos <= cll.length:\n    cll.insert_at(pos, data)\nelse:\n    raise IndexError('position out of range')","handlingStrategy":"validation","validationCode":"def can_insert(cll, idx) -> bool:\n    return 0 <= idx <= cll.length","typeGuard":null,"tryCatchPattern":"try:\n    cll.insert_at(idx, data)\nexcept Exception:\n    raise IndexError(f'insert position {idx} out of range') from None","preventionTips":["Check 0 <= idx <= length before inserting","Never pass -1 from a failed search directly as an index","Use length-based loops, not hardcoded positions"],"tags":["linked-list","index-out-of-range","data-structure"],"backgroundTag":"index-out-of-bounds","analyzedSha":"40f4cd2652d75ef8e49d76e5c4d431d458712719","analyzedAt":"2026-08-27T11:12:20.313Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}