Hacker News new | ask | show | jobs
by stakecounter 3544 days ago
Wouldn't this just simplify to O(n)? Consider a case where the keys are objects whose hashcode() is overridden to return a constant (while equals() still uses reference equality from Object.equals).
1 comments

http://stackoverflow.com/questions/4553624/hashmap-get-put-c...

It's a dumb question to be authoritative about because there are different implementations of hashmap and hashcode meaning that you can't guarantee the runtime complexity.

According to the SO answer above Java8 will be mostly O(1) but full buckets may be stored as trees meaning that they will be O(log n). But as the answer says

"So no, O(1) certainly isn't guaranteed - but it's usually what you should assume when considering which algorithms and data structures to use."

The interviewer could have explored the candidates knowledge of how hashmap is implemented, or could be implemented, which would be more rewarding than insulting and gloating.