Interview questions
How does HashMap work internally?
How I'd answer:
"A HashMap is an array of buckets whose length is a power of two, 16 by default. To put a key, it takes the key's hashCode, XORs the high 16 bits into the low
16 to spread them, and ANDs with capacity minus one to get the bucket index — the AND replaces a modulo because the capacity is a power of two. If the bucket
is empty, it stores a node with the hash, key, value and a next pointer. If not, it walks the bucket comparing the stored hash and then equals; an equal key has
its value replaced, otherwise a node is appended. get follows the same path. Collisions are chained in a linked list, and since Java 8 a bucket with 8 or more
entries, in a table of at least 64, is converted to a red-black tree, so the worst case per bucket is O(log n) instead of O(n). When size exceeds capacity times
the load factor 0.75, the table doubles and entries are redistributed — each one either stays at its index or moves to index plus old capacity. Average operations
are O(1), and resizing is amortized. It allows one null key, isn't ordered and isn't thread-safe — that's ConcurrentHashMap. And it only works correctly if keys
honour the equals/hashCode contract and don't change while in the map."
Combines: Ch 4; Object-Oriented Programming Ch 8; Big-O and Complexity Ch 7.
ArrayList vs LinkedList — when would you use each?
How I'd answer: "ArrayList is backed by a resizable array: O(1) random access, amortized O(1) appends, but O(n) inserts and removals at the front or middle because elements shift. LinkedList is a doubly linked list: O(1) insert and remove at the ends or at an iterator position, but O(n) to reach an index. In practice I almost always use ArrayList: LinkedList allocates a node object per element, uses several times more memory, and its scattered nodes cause cache misses, so it's often slower even where Big-O favours it. If I need efficient adds and removes at both ends, ArrayDeque is better than LinkedList."
Combines: Ch 2, 8; How Computers Work Ch 6.
HashMap vs LinkedHashMap vs TreeMap?
How I'd answer:
"All three are maps. HashMap gives O(1) average operations with no ordering guarantee. LinkedHashMap adds a doubly linked list across entries, so it iterates in
insertion order — or access order if configured, which makes it a simple LRU cache with removeEldestEntry — at a small memory cost, still O(1). TreeMap is a
red-black tree: keys are always sorted and it supports floor, ceiling and range views, but operations are O(log n), keys must be comparable, and it uses the
comparator rather than equals to decide key identity. I pick HashMap by default, LinkedHashMap when iteration order matters, and TreeMap when I need sorted keys
or nearest-key queries."
Combines: Ch 4, 5, 6.
How would you find the 10 most frequent words in a very large text?
How I'd answer:
"First count with a HashMap from word to count — O(n) over the words, using merge(word, 1, Integer::sum). Then keep a min-heap of size 10 ordered by count: for
each entry, offer it, and if the heap grows past 10, poll the smallest. The heap's root is always the weakest of the current top 10. That's O(m log 10) for m
distinct words — effectively linear — with O(10) heap space, instead of sorting all m entries in O(m log m). At the end I poll the heap and reverse to get
most-frequent first. For ties I'd put a tie-breaker in the comparator, like alphabetical order, and I'd use Integer.compare rather than subtraction."
Combines: Ch 4, 7, 9.
What is a ConcurrentModificationException and how do you avoid it?
How I'd answer:
"Java's standard collections have fail-fast iterators: they keep a modification count and throw ConcurrentModificationException if the collection is structurally
changed during iteration by anything other than the iterator itself. The usual cause is removing from a list inside a for-each loop — it's usually single-threaded,
not a concurrency bug. To remove while iterating, I use iterator.remove(), or more simply removeIf with a predicate, or iterate backwards by index. If multiple
threads really share the collection, I'd use a concurrent collection like ConcurrentHashMap or CopyOnWriteArrayList, whose iterators don't throw it."
Combines: Ch 3.