logoalt Hacker News

brody_hameryesterday at 10:00 PM1 replyview on HN

Yea if I cast the large calculated integers to strings, performance is O(1)

`values = [str(i * M) for i in range(1, n + 1)]`

or

`values = [i * M % 1_000_000_000_000_000 for i in range(1, n + 1)]`


Replies

jlduggeryesterday at 11:38 PM

Part of the secret explained elsewhere on this HN post is that the OP is selecting values that all collide. Most hash tables handle collisions with linked lists that would be linear insert. It's O(1) average case but O(n) if you pull an "oops all collisions on the same bucket" stunt.