Twelve Then Twenty Four

Java's HashMap documentation states that when the number of entries exceeds the product of the load factor and the current capacity, the table is rehashed to roughly twice the number of buckets. It also states that if the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash ever happens. HashMap uses a default capacity of 16, a default load factor of 0.75, and always rounds a requested capacity up to a power of two.

You run:

Map<String, Integer> m = new HashMap<>();
for (int i = 0; i < 40; i++) m.put("key" + i, i);

How many times does the table resize, and what initial capacity passed to the constructor would have avoided every resize?

A

1 resize (on the 13th insertion); an initial capacity of 32 avoids it

B

2 resizes (on the 13th and 25th insertions); an initial capacity of 64 avoids both

C

2 resizes; an initial capacity of 30 avoids both, since 30 is 40 times the 0.75 load factor

D

3 resizes (13th, 25th, 49th); an initial capacity of 64 avoids all three