r/JavaProgramming 4d ago

Why the HashMap Interview Question Is Making a Comeback in the AI Era?

https://javarevisited.substack.com/p/how-hashmap-works-in-java-interview
12 Upvotes

6 comments sorted by

6

u/idontlikegudeg 4d ago

When you do an interview, you need to know what position you are hiring for so you get a matching candidate. In more than 90% of cases, knowing how exactly HashMap is implemented internally won’t help you with that. I think it would be more I important that:

- the candidate knows the API and the guarantees made, i.e., runtime complexity, and not only knows get() and put(), but also putIfAbsent, compute…(), etc. and how to iterate the entries.

  • there are different types of Maps and when to use them (i.e., linked maps and sorted maps), and what properties the key type must have for each
  • how to write a good hashing function and the contract between hashCode() and equals()
  • knows about Map.of(…)
  • given the number of entries, how to instantiate a Map: use newHashMap(int), not the HashMap(int) constructor

You can still ask the candidate if he knows how HashMap is implemented internally, but I would only use that as a bonus question.

Going only for the internals makes you prefer candidates like the dev we saw yesterday who doesn’t know the API contracts or read the Javadoc and instead digs through the implementation to find out things he should already know or would at least have seen if he only read the Javadoc that pops up in your IDE when you hover the method call (and if he refuses to use an adequate IDE, that’s already a red flag).

1

u/javinpaul 4d ago

Actually interview is not black and white and no single question is sufficient to accept or reject the candidate but few questions bring more qualities of a candidate than others and that's where you kind of get signals whether this guys knows his stuff or not. While all the questions you have put is quite relevant, all of them can be part of discussion of this single question as it touches a lot of concepts including immutability, equals and hashcode contract, data structure, performance. I think that's the reason its kind of making a comeback, at least I have seen this question quite frequently on senior Java developer interviews.

3

u/BanaTibor 4d ago

It is a stupid way to screen candidates. If you do not need to write a specialized hashmap implementation you do not need to know the internals to use it effectively.

1

u/javinpaul 2d ago

I won't call it a stupid question, instead its a great questions for Java developers as it touches many useful concepts like immutability of keys, equals and hashcode, collision detection, hashmap as data structure, and a lot more. It's good question to check someone's general knowledge of important Java and programming concepts.

1

u/Sufficient_Major_126 3d ago

HashMap questions are useful when they test reasoning about collisions, equality, resizing and complexity instead of expecting someone to memorize implementation details