A HashMap holds an array of buckets. The key hash is spread by mixing in its high bits, then the index is that hash masked against the table length, always a power of two. Collisions form a linked list in the bucket, and a bin with at least eight entries in a table of at least sixty four converts to a red black tree. Past the 0.75 load factor the table doubles and entries are rehashed.
Why interviewers ask this
The interviewer is testing whether you understand the data structure you use in every class rather than reciting that it is fast. The treeification and resize details show current knowledge, since the implementation changed in Java 8. It also leads naturally to why keys should be immutable, why a poor hash degrades performance, and why HashMap is unsafe under concurrent writes.
How to structure your answer
- Describe the bucket array and how an index is derived.
- Explain collision handling and the switch from list to tree.
- Cover load factor, resizing and rehashing cost.
- Draw the practical conclusions about key design and thread safety.
Example answer
Internally it is an array of bins. The key's hashCode is spread by exclusive oring the high bits down, which matters because the index is just the hash masked with table length minus one, so without that mixing only the low bits would ever be used. Entries that collide chain in that bin, and since Java 8 a bin that grows past eight entries, when the table is at least sixty four, turns into a red black tree so worst case lookup is logarithmic instead of linear. That was a response to hash collision denial of service attacks. Growth is driven by the load factor, so at 0.75 occupancy the table doubles and everything is redistributed, which is why sizing the map up front matters if I know it will hold a million entries. The practical consequences I care about are that keys should be immutable, that a bad hashCode makes everything land in one bin, and that concurrent writes can corrupt the table, so shared maps get ConcurrentHashMap rather than external locking.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- Why is the table size always a power of two?
- What does ConcurrentHashMap do differently to allow concurrent writes?
- When would you prefer LinkedHashMap or TreeMap?
Related java developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions