Maps: interview questions & scenarios
These are the map questions I ask when I want to know whether you can picture the table, the bucket and the equality contract behind a one-line lookup. Answer it out loud first, then open the model answer, and use the deeper lessons when you want the full path: How HashMap works, Hashing, collisions, treeification and load factor, LinkedHashMap and LRU caches, TreeMap and red-black trees, special maps, concurrent maps and everyday map operations.
Walk me through HashMap.put and HashMap.get.
Tests hash spreading, bucket search, equals contract
Show answerHide answer
Model answer
A HashMap is an array of bins. On put, Java 21 computes a spread hash, chooses a bin with (n - 1) & hash, and either stores the first node, updates an equal key, appends to the bin list, or inserts into a tree bin. For a new key it increments size, and if size passes capacity * loadFactor, it resizes.
get follows the same route without mutation: spread hash, mask to the bin, check the first node, then search the list or tree by stored hash, ==, and finally equals. The hash narrows the search. equals decides the key match.
How I’d say it in the interview
"HashMap first turns the key's hashCode into a better spread hash, masks it into a bucket, then checks candidates in that bucket by hash, identity and equals. Put either updates the existing node or adds a new one, and resize happens after the new size crosses the threshold. Get repeats the same path and searches only that one bin."
Signals depth
Saying that hashCode is not the identity of the key. Colliding keys can live in the same bin because equals separates them.
Red flags
"It hashes the key and jumps straight to the value" with no mention of collisions or equality.
Follow-ups interviewers ask next
What if two different keys have the same hash?
They land in the same bin. Both entries remain valid as long as
equalssays they are different.What if
equalsis expensive?The stored hash check avoids most
equalscalls. A bad hash function still hurts because it sends too many candidates into the same bin.
Why is the capacity a power of two, and what is the point of hash spreading?
Tests bit masking, low bits, table sizing
Show answerHide answer
Model answer
HashMap keeps the table length as a power of two so it can compute the index with (n - 1) & hash instead of % n. With n = 16, the mask keeps the low 4 bits. That is fast, non-negative and lets resizing split bins cleanly.
The catch is that many hashCode() implementations have useful information in high bits too. Java spreads with h ^ (h >>> 16), so high bits influence the low bits before the mask is applied. It is not a cryptographic hash. It is a cheap guard against common weak distributions.
How I’d say it in the interview
"Power-of-two capacity makes the bucket index a mask: (n - 1) & hash. Since that mostly looks at low bits, HashMap mixes the high 16 bits into the low 16 with an XOR. That keeps the index cheap without trusting every key's low bits to be good."
Signals depth
Mentioning that constructor capacities are rounded up, so new HashMap<>(100) gets a 128-bucket table when allocated.
Red flags
"Power of two prevents collisions." It does not. It makes indexing and resize mechanics efficient.
Follow-ups interviewers ask next
What if the table length were not a power of two?
You would need modulo or a different reduction step, and the Java 8 resize split would not be the same one-bit test.
Explain treeification, load factor 0.75, and the resize split.
Tests tree bins, thresholds, resize mechanics
Model answer, how to say it, 2 follow-ups: unlock with the Java Pass
A HashMap lookup starts failing after a key is changed. Debug it.
Tests mutable keys, stored hash, remove failure
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What does this print when equals is overridden but hashCode is not?
Tests equals-hashCode contract, duplicate logical keys
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Design an LRU cache in Java.
Tests LinkedHashMap, access order, eviction
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
When would you choose HashMap, LinkedHashMap or TreeMap?
Tests ordering, range queries, access pattern
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
You need the closest stock prices higher and lower than a target. Which map do you use?
Tests NavigableMap, floorEntry, ceilingEntry
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Build a leaderboard sorted by score, including ties.
Tests TreeMap, duplicate scores, reverse order
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Pick the right special map: weak-key cache or reference-equality cache.
Tests WeakHashMap, IdentityHashMap, reference equality
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Compare ConcurrentHashMap, Collections.synchronizedMap and Hashtable. Why does ConcurrentHashMap reject null?
Tests locking, compound actions, null ambiguity
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
A shared counter map loses increments. What's wrong with get plus put?
Tests compound race, merge, LongAdder
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Why is entrySet() usually better than keySet() for iterating keys and values?
Tests map iteration, lookup cost, Map.Entry
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What happens when Collectors.toMap sees duplicate keys?
Tests streams, duplicate key merge function
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Premium question set
Keep reading with the Java Pass
The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.
From ₹199 · one-time payment · No auto-renewal