Skip to content
Sharp

Engineering Interviews · interview question

When reviewing code for concurrency issues, why is using a synchronized block with a HashMap not recommended?

Multiple choiceEasychapter-20-curatedpremiumengineering-interviewscode-review-spotting-bugs
java
HashMap<String, Integer> map = new HashMap<>();
// synchronization attempt
synchronized(map) { map.put("key", 1); }
Choose one answer
Answer and explanation

Correct answer

C. It does not eliminate race conditions.

Why

HashMap is not thread-safe, and using a synchronized block does not inherently solve all concurrency issues like race conditions. Instead, consider using ConcurrentHashMap for thread safety.

Test yourself on Engineering Interviews

A free test with instant, explained results. No account needed.

Start free test
All Engineering Interviews questions →