{"record":{"id":"0c6aab84d72cad52","repo":"donnemartin/interactive-coding-challenges","slug":"word-does-not-exist-0c6aab","errorCode":null,"errorMessage":"word does not exist","messagePattern":"word does not exist","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"graphs_trees/trie/trie_solution.ipynb","lineNumber":226,"sourceCode":"    \"    def insert(self, word):\\n\",\n    \"        if word is None:\\n\",\n    \"            raise TypeError('word cannot be None')\\n\",\n    \"        node = self.root\\n\",\n    \"        parent = None\\n\",\n    \"        for char in word:\\n\",\n    \"            if char in node.children:\\n\",\n    \"                node = node.children[char]\\n\",\n    \"            else:\\n\",\n    \"                node.children[char] = Node(char, parent=node)\\n\",\n    \"                node = node.children[char]\\n\",\n    \"        node.terminates = True\\n\",\n    \"\\n\",\n    \"    def remove(self, word):\\n\",\n    \"        if word is None:\\n\",\n    \"            raise TypeError('word cannot be None')\\n\",\n    \"        node = self.find(word)\\n\",\n    \"        if node is None:\\n\",\n    \"            raise KeyError('word does not exist')\\n\",\n    \"        node.terminates = False\\n\",\n    \"        parent = node.parent\\n\",\n    \"        while parent is not None:\\n\",\n    \"            # As we are propagating the delete up the \\n\",\n    \"            # parents, if this node has children, stop\\n\",\n    \"            # here to prevent orphaning its children.\\n\",\n    \"            # Or\\n\",\n    \"            # if this node is a terminating node that is\\n\",\n    \"            # not the terminating node of the input word, \\n\",\n    \"            # stop to prevent removing the associated word.\\n\",\n    \"            if node.children or node.terminates:\\n\",\n    \"                return\\n\",\n    \"            del parent.children[node.key]\\n\",\n    \"            node = parent\\n\",\n    \"            parent = parent.parent\\n\",\n    \"\\n\",\n    \"    def list_words(self):\\n\",\n    \"        result = []\\n\",","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/trie/trie_solution.ipynb#L208-L244","documentation":"Trie.remove raises KeyError('word does not exist') when find(word) returns None, meaning no terminating path spells the word. This includes words never inserted, words already removed, and prefixes that exist as node paths but not as complete words (terminates is False).","triggerScenarios":"Calling trie.remove('foo') when 'foo' was never inserted; removing a word twice; removing a bare prefix like 'ab' when only 'abc' was inserted.","commonSituations":"Idempotent deletion code that assumes remove is safe to call repeatedly; deleting user-supplied words without checking membership; race where another thread already removed the word.","solutions":["Check membership first: if trie.find(word) is not None: trie.remove(word), or note that find alone doesn't confirm termination — use a contains-style check","Wrap remove in try/except KeyError for idempotent deletes","Verify the word was actually inserted (and is a full word, not a prefix) before removal"],"exampleFix":"// before\ntrie.remove('foo')  # KeyError if absent\n// after\ntry:\n    trie.remove('foo')\nexcept KeyError:\n    pass  # already absent; deletion is idempotent","handlingStrategy":"try-catch","validationCode":"if trie.find(word) is not None:\n    trie.remove(word)","typeGuard":"def is_stored_word(trie, word) -> bool:\n    node = trie.find(word)\n    return node is not None and node.terminates","tryCatchPattern":"try:\n    trie.remove(word)\nexcept KeyError:\n    pass  # already absent; keep deletes idempotent","preventionTips":["Make delete flows idempotent by catching or pre-checking membership","Remember prefixes of longer words are not themselves stored words"],"tags":["trie","keyerror","deletion","python"],"backgroundTag":"key-not-found","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}