{"record":{"id":"e9fd494f2fe2a27d","repo":"TheAlgorithms/Java","slug":"position-out-of-bounds-e9fd49","errorCode":null,"errorMessage":"Position out of bounds","messagePattern":"Position out of bounds","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/lists/CircularDoublyLinkedList.java","lineNumber":106,"sourceCode":"                sb.append(\", \");\n            }\n            current = current.next;\n        }\n        sb.append(\" ]\");\n        return sb.toString();\n    }\n\n    /**\n     * Removes and returns the element at the specified position in the list.\n     * Throws an IndexOutOfBoundsException if the position is invalid.\n     *\n     * @param pos the position of the element to remove\n     * @return the value of the removed element - pop operation\n     * @throws IndexOutOfBoundsException if the position is out of range\n     */\n    public E remove(int pos) {\n        if (pos >= size || pos < 0) {\n            throw new IndexOutOfBoundsException(\"Position out of bounds\");\n        }\n        Node<E> current = head.next;\n        for (int i = 0; i < pos; i++) {\n            current = current.next;\n        }\n        current.prev.next = current.next;\n        current.next.prev = current.prev;\n        E removedValue = current.value;\n        size--;\n        return removedValue;\n    }\n}\n","sourceCodeStart":88,"sourceCodeEnd":119,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/lists/CircularDoublyLinkedList.java#L88-L119","documentation":"Thrown by CircularDoublyLinkedList.remove(int pos) when pos >= size or pos < 0. The list uses 0-based positions and the guard prevents walking past the sentinel/head node. Removing an out-of-range index would corrupt the circular linkage.","triggerScenarios":"Calling remove on an empty list. Calling remove(size). Negative pos passed from an underflowed calculation.","commonSituations":"Index sourced from a different collection's size. Stale index held across prior removes. Off-by-one loop bound `i <= size`.","solutions":["Check `pos >= 0 && pos < list.size()` before remove.","Guard the empty-list case explicitly.","Recompute the index or iterate in reverse when removing multiple elements.","Use strict `<` in loop bounds, never `<=` against size."],"exampleFix":"// before\nlist.remove(pos); // pos may be stale\n\n// after\nif (pos >= 0 && pos < list.size()) {\n    list.remove(pos);\n}","handlingStrategy":"validation","validationCode":"if (pos >= 0 && pos < list.size()) {\n    return list.remove(pos);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return list.remove(pos);\n} catch (IndexOutOfBoundsException e) {\n    // invalid pos\n}","preventionTips":["Validate position against [0, size-1] before remove.","Recompute size during multi-remove loops.","Avoid stale indices held across structural changes."],"tags":["linked-list","index-out-of-bounds","data-structure","off-by-one","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}