{"record":{"id":"14d4e3096cf3c1e2","repo":"TheAlgorithms/Go","slug":"deleting-out-of-bounds-key","errorCode":null,"errorMessage":"deleting out of bounds key","messagePattern":"deleting out of bounds key","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"structure/tree/btree.go","lineNumber":198,"sourceCode":"\t\ttree.root = NewBTreeNode[T](tree.maxKeys, true)\n\t\ttree.root.keys[0] = key\n\t\ttree.root.numKeys = 1\n\t\treturn\n\t}\n\n\tif tree.root.IsFull(tree.maxKeys) {\n\t\tnewRoot := NewBTreeNode[T](tree.maxKeys, false)\n\t\tnewRoot.numKeys = 0\n\t\tnewRoot.children[0] = tree.root\n\t\tnewRoot.Split(0, tree.maxKeys)\n\t\ttree.root = newRoot\n\t}\n\ttree.root.InsertNonFull(tree, key)\n}\n\nfunc (node *BTreeNode[T]) DeleteIthKey(i int) {\n\tif i >= node.numKeys {\n\t\tpanic(\"deleting out of bounds key\")\n\t}\n\tfor j := i; j < node.numKeys-1; j++ {\n\t\tnode.keys[j] = node.keys[j+1]\n\t\tnode.children[j+1] = node.children[j+2]\n\t}\n\tnode.numKeys--\n}\n\n// Transform:\n//\n//\tA B C\n//\t / \\\n//\ta   b\n//\n// Into:\n//\n//\tA C\n//\t |","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/tree/btree.go#L180-L216","documentation":"BTreeNode.DeleteIthKey panics when i >= numKeys: the caller asked to delete an index beyond the keys stored in the node, an internal invariant violation from B-tree Delete/Merge logic rather than user input.","triggerScenarios":"Calling node.DeleteIthKey(i) with i equal to or beyond numKeys; Merge passing an idx that is out of range after prior mutations; off-by-one in custom delete code that computes the key index.","commonSituations":"Direct manipulation of nodes in tests; deleting from an empty node (numKeys == 0, any i fails); stale index retained after earlier deletions shifted keys.","solutions":["Fix the caller (Merge/Delete) to only pass indexes < numKeys","Return an error instead of panicking if this is used as a public API","Add bounds checks and unit tests covering boundary indexes in Delete"],"exampleFix":"// before\nnode.DeleteIthKey(i) // i may be out of range\n// after\nif i >= 0 && i < node.numKeys {\n    node.DeleteIthKey(i)\n}","handlingStrategy":"validation","validationCode":"if node.numKeys == 0 || i < 0 || i >= node.numKeys {\n    return errors.New(\"key index out of range\")\n}\nnode.DeleteIthKey(i)","typeGuard":null,"tryCatchPattern":"func safeDeleteIth(n *BTreeNode[int], i int) (err error) {\n    defer func() {\n        if r := recover(); r != nil { err = fmt.Errorf(\"delete failed: %v\", r) }\n    }()\n    n.DeleteIthKey(i)\n    return nil\n}","preventionTips":["Recompute key indices after every mutation","Check numKeys before deleting","Use tree.Delete rather than manual node surgery"],"tags":["panic","btree","out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}