🗂️ Collections and Generics — interview questions
11 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.
11 of 11
01A teammate wants to switch a `HashMap<String, Order>` to a `TreeMap` because "sorted is safer than random." What's the actual criterion for choosing between them, and what does the switch cost?Mapseasy0-2y02A `Set<Customer> seen = new HashSet<>()` is used to deduplicate customers arriving from an import job. `Customer` has no overridden `equals` or `hashCode`. The set ends up with more entries than there are distinct customers. Why, mechanically, and what's the minimum fix?Setsmedium0-2y03"ArrayList or LinkedList?" — a teammate picks LinkedList because the service inserts into a list a lot and the textbook says LinkedList is O(1) for insertion. What's actually true about that, and what should they use instead?Listsmedium0-2y04A method returns `Collections.unmodifiableList(orders)` so callers "can't mutate the list." Weeks later, a caller reports the returned list changed underneath them mid-iteration. What actually happened, and how is `List.of(...)` different?Immutable collectionsmedium3-5y05A loop that removes expired sessions from a `List<Session>` inside a `for-each` sometimes throws `ConcurrentModificationException` and sometimes silently leaves one expired session behind, with no exception, on the same codebase. There's only one thread. Explain both outcomes from the same mechanism.Iterationmedium3-5y06A service does `List<User> users = mapper.readValue(json, List.class);`. It compiles with an unchecked warning nobody reads, passes every test that checks `users.size()`, and then three files away, `users.get(0).getName()` throws `ClassCastException: LinkedHashMap cannot be cast to User`. Explain exactly what happened, and why the exception fires three files from the actual mistake.Genericshard3-5y07A reporting job builds `Set<Customer> sorted = new TreeSet<>(Comparator.comparing(Customer::signedUp)); sorted.addAll(customers);` to sort and deduplicate customers by id in one step. The export has 6,000 rows where the source had 10,000. Nothing threw. What went wrong, and what's the actual rule being violated?Orderinghard5-8y08A generic `copyAll(Collection<T> from, Collection<T> to)` method fails to compile when called with a `List<Integer>` source and a `List<Number>` destination, even though copying integers into a list of numbers is obviously safe. Fix the signature, and explain exactly why each wildcard you add is necessary — not just "PECS," the actual reasoning.Genericshard5-8y09A log pipeline needs the 10 slowest requests out of 100 million latency samples streaming past, without holding all 100 million in memory. Someone suggests a max-heap so the largest values are easy to find. What's actually the right structure, and why is a max-heap the wrong intuition here?Queueshard5-8y10`List<?> xs` cannot have any non-null element added to it, even `xs.set(0, xs.get(0))` — putting back exactly what was just read out fails to compile. But a private helper method with a type parameter can swap two elements of the same `List<?>` just fine. Explain the compiler mechanism that makes the second case work, precisely — not just "it captures the type."Genericssenior8+y11A `HashMap` doubles its table when the load factor is exceeded. Someone assumes that means every key gets rehashed — `hashCode()` called again for each one — during a resize. Is that true, and if not, what does a resize actually do, and why does it matter for a key whose `hashCode()` is expensive to compute?Mapssenior8+y