{"record":{"id":"535861fab0a6ec54","repo":"donnemartin/interactive-coding-challenges","slug":"key-not-found","errorCode":null,"errorMessage":"Key not found","messagePattern":"Key not found","errorType":"validation","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"arrays_strings/hash_map/hash_map_solution.ipynb","lineNumber":149,"sourceCode":"    \"        self.table = [[] for _ in range(self.size)]\\n\",\n    \"\\n\",\n    \"    def _hash_function(self, key):\\n\",\n    \"        return key % self.size\\n\",\n    \"\\n\",\n    \"    def set(self, key, value):\\n\",\n    \"        hash_index = self._hash_function(key)\\n\",\n    \"        for item in self.table[hash_index]:\\n\",\n    \"            if item.key == key:\\n\",\n    \"                item.value = value\\n\",\n    \"                return\\n\",\n    \"        self.table[hash_index].append(Item(key, value))\\n\",\n    \"\\n\",\n    \"    def get(self, key):\\n\",\n    \"        hash_index = self._hash_function(key)\\n\",\n    \"        for item in self.table[hash_index]:\\n\",\n    \"            if item.key == key:\\n\",\n    \"                return item.value\\n\",\n    \"        raise KeyError('Key not found')\\n\",\n    \"\\n\",\n    \"    def remove(self, key):\\n\",\n    \"        hash_index = self._hash_function(key)\\n\",\n    \"        for index, item in enumerate(self.table[hash_index]):\\n\",\n    \"            if item.key == key:\\n\",\n    \"                del self.table[hash_index][index]\\n\",\n    \"                return\\n\",\n    \"        raise KeyError('Key not found')\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]\n  },\n  {","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/arrays_strings/hash_map/hash_map_solution.ipynb#L131-L167","documentation":"Raised by HashMap.get when the key is not present in the bucket its hash maps to. This open hashing (chaining) implementation returns the value only on an exact item.key match; otherwise the miss escalates to KeyError rather than returning None.","triggerScenarios":"Calling get on a removed or never-set key; getting a key whose hash bucket was searched without a match (collision chains included).","commonSituations":"Assuming a default return value like dict.get; typos or case mismatches in keys; reading after concurrent/logical removal.","solutions":["Check the key with in-style logic or catch KeyError instead of assuming a None default","Use a wrapper that returns a default: return map.get(k) if present else default","Verify the key was actually set (logging set/remove calls) before reading"],"exampleFix":"# before\nvalue = hmap.get(key)  # raises KeyError on miss\n\n# after\ntry:\n    value = hmap.get(key)\nexcept KeyError:\n    value = 'default'","handlingStrategy":"try-catch","validationCode":"try:\n    value = hmap.get(key)\nexcept KeyError:\n    value = None","typeGuard":"def key_exists(hmap, key) -> bool:\n    try:\n        hmap.get(key)\n        return True\n    except KeyError:\n        return False","tryCatchPattern":"try:\n    value = hmap.get(key)\nexcept KeyError:\n    value = default_value","preventionTips":["Do not assume dict.get-style None defaults; this map raises","Wrap access with a default-returning helper"],"tags":["hash-map","keyerror","key-not-found"],"backgroundTag":"key-not-in-collection","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}