Skip to content

Fix HashMap duplicate keys and silent delete after deletions - #15796

Open
JayPokale wants to merge 1 commit into
TheAlgorithms:masterfrom
JayPokale:fix-hash-map-reinsert-after-delete
Open

JayPokale wants to merge 1 commit into
TheAlgorithms:masterfrom
JayPokale:fix-hash-map-reinsert-after-delete

Conversation

@JayPokale

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

data_structures/hashing/hash_map.py::HashMap mishandles deleted slots in two ways.

  1. _add_item stored the value in the first free-looking slot on the probe path, and a deleted slot counts as free. If a colliding key had been deleted earlier, setting a key that still sits further along the path inserted a second copy instead of updating it. On master: hm[0] = 1; hm[8] = 2; del hm[0]; hm[8] = 3 gives len(hm) == 2, and after del hm[8] the key is still in hm. _add_item now looks for the key along the path first (stopping at a never-used slot) and only then takes the first free slot.
  2. __delitem__ raised KeyError only when it reached a never-used slot. After enough inserts and deletes every bucket is either an item or a deleted marker, so the loop ended without finding the key, del hm[missing] silently succeeded, and the map could resize down. The loop now raises KeyError when it has visited every bucket without a match.

I added doctests for both; they fail on master, and I kept them in this PR as the regression evidence for these two fixes. I compared the class with a dict over random sets and deletes of keys 0..15 (600 sequences of up to 60 operations per seed, checking return values, len and the items after every step): after the change there are no mismatches over four seeds, while master showed wrong len and wrong delete results. ruff check, ruff format --check and python -m doctest pass.

Written with the help of Claude Code (Claude Sonnet 5.5).

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant