Recently, I attended an interview and the interviewer asked me this question.
How many times rehashing can occur in an HashMap once? twice? or N number of times? [when (threshold value +1) elements are added each time]
I know that when an map is full, then its likely that we are doing something wrong.
I admit that I could not give a satisfactory answer. Can anyone tell me the approach to answer such questions.
Or, what exactly was the interviewer looking for in an convincing answer?
Below are some of the SO questions I referred before asking this question.
https://stackoverflow.com/a/28811708 "Rehashing of a hash map is done when the number of elements in the map reaches the threshold values" Load factor of a HashMap is 0.75 and the default initial capacity value is 16. Once the number of elements reaches or crosses 12 element capacity rehashing of map happens.
When the number of entries in the hash map exceeds the product of the load factor and the current capacity, the hash map is rehashed (internal data structures are rebuilt), so that the hash map has approximately twice the number of buckets. When you rehash and move everything to a new location (bucket, etc.) then the older elements are also rehashed again and stored in the new bucket according to their new hash codes. The old space which was allocated to store the elements is garbage collected.