Most "explain HashMap" interview questions are dead now — anyone
can look that up. What separates candidates today is scenario
reasoning: given a real constraint, which collection do you reach
for, and why not the obvious one?
12 questions worth testing yourself on:
You need an LRU cache that evicts the least recently used entry
automatically when full. Which collection do you reach for?A producer thread generates data faster than the consumer can
process it, and the app is running out of memory. What do you
change?You need the top 10 highest scores out of 1 million incoming
values, without holding all 1 million in memory. How?Multiple threads read a config map constantly, but writes
happen once a day. ConcurrentHashMap, synchronizedMap, or
something else entirely?You store a mutable object as a HashMap key, then modify one
of its fields after inserting it. What breaks?Every key in your HashMap somehow returns the same hashCode().
What happens to lookup performance, and why?You remove an element from a List while iterating it with a
for-each loop. What exception do you get — and why does it happen
even in single-threaded code?Why does iterator.remove() avoid the exception in Q7, but
list.remove() inside the same loop doesn't?What's the real difference between a fail-fast and a fail-safe
iterator — and where do ArrayList, HashMap, ConcurrentHashMap, and
CopyOnWriteArrayList each fall?You need a Set that stays sorted at all times and supports
range queries — "give me everything between X and Y." Which
collection, and why not just sort a HashSet every time you need it?A teammate says LinkedList is always faster for insertion than
ArrayList. Is that actually true in production?Your CopyOnWriteArrayList is quietly driving up memory and GC
pressure. Why — and when should you actually reach for it?
How many could you answer confidently without looking anything up?
I've written out full answers with code and the reasoning behind
each one in my guide, alongside Java 8-21, Multithreading, Spring
Boot, Microservices, Design Patterns, and Coding Round Patterns.
Free sample: https://drive.google.com/file/d/1u3PQbTY1gLn34UmWG7Cxx4cmdibD2dvU/view?usp=sharing
Full guide: https://kamaninikhil.gumroad.com/l/java-interview-guide
Top comments (0)