π₯ Top 15+ Tricky HashMap Interview Questions (With Smart Answers)
Whether you're preparing for interviews at Amazon, Google, TCS, Infosys, Wipro, or Capgemini, or simply leveling up your Java skills, understanding HashMap internals is a must. Let's explore the trickiest HashMap interview questionsβwith crisp, smart answers that impress interviewers.
π 1. Can HashMap have null
keys and null
values?
π§ Trick: Many think null
is never allowed.
β Answer:
-
Yes, 1 null key is allowed.
-
Multiple null values are allowed.
π 2. What happens when you insert duplicate keys?
π§ Trick: Interviewers expect you to explain key replacement.
β
Answer:
The latest value overrides the old value for the same key.
π 3. Is HashMap thread-safe?
π§ Trick: If you say yes β thatβs a red flag.
β
Answer:
β No, it is not thread-safe. Use:
-
Collections.synchronizedMap()
(slower, locks whole map) -
ConcurrentHashMap
(preferred in multi-threading)
π 4. Can two unequal objects have the same hash code?
π§ Trick: This is about collision handling.
β
Answer:
Yes! It's called a hash collision.
Java handles collisions using:
-
Linked List (Java 7)
-
Red-Black Tree (Java 8+)
π 5. What happens internally when put()
is called?
π§ Trick: They want step-by-step internal flow.
β Answer (Java 8):
-
Compute hash using
hashCode()
-
Map hash to bucket index
-
Check if key exists using
equals()
-
If exists β replace value
-
If not β create new Node
-
If collisions > 8 β convert bucket to Red-Black Tree
π 6. What is the load factor?
π§ Trick: Misunderstanding load factor leads to wrong resizing logic.
β
Answer:
Load factor = threshold to resize
-
Default =
0.75f
-
Resize happens when:
current size β₯ capacity * loadFactor
π 7. What is rehashing?
π§ Trick: Interviewers want to test your understanding of resizing.
β
Answer:
When capacity threshold is crossed, HashMap creates a new array (double size) and repositions all existing keys (rehashing).
π 8. Can HashMap key be mutable?
π§ Trick: Many devs unknowingly use mutable objects as keys.
β
Answer:
Technically yes, but it's a bad practice.
If a keyβs hashCode changes after insertion, you won't be able to retrieve the value.
π 9. Difference between HashMap, TreeMap, and LinkedHashMap?
Feature | HashMap | TreeMap | LinkedHashMap |
---|---|---|---|
Ordering | β No order | β Sorted | β Insertion order |
Null Key | β One | β Not allowed | β One |
Thread-safe | β No | β No | β No |
π 10. What is treeification in Java 8?
π§ Trick: This tests Java 8 internal knowledge.
β
Answer:
If one bucket has more than 8 entries and capacity β₯ 64, Java 8 converts the bucket into a Red-Black Tree β improves lookup to O(log n)
π 11. Whatβs the worst-case time complexity of get()
and put()
?
Scenario | Java 7 | Java 8 |
---|---|---|
Best case (no collision) | O(1) | O(1) |
Worst case (collision) | O(n) | O(log n) |
π 12. What causes ConcurrentModificationException
?
π§ Trick: Simple-looking question, but many fail it.
β
Answer:
Modifying a HashMap while iterating it using for-each loop (without Iteratorβs remove()
).
π 13. Can initial capacity be set to a large value like Integer.MAX_VALUE
?
β
Answer:
You can, but itβs risky:
-
Might throw
OutOfMemoryError
-
Wastes memory if not filled
π 14. Why should hashCode()
and equals()
be overridden for custom keys?
π§ Trick: Real-world trap question.
β
Answer:
HashMap uses:
-
hashCode()
β to find bucket -
equals()
β to find key in that bucket
If not overridden, keys wonβt match, leading to unexpected behavior.
π 15. Can a HashMap have two keys with same hash code?
β
Answer:
Yes. HashMap handles collisions using chaining.
No comments:
Post a Comment