Lists
24 questions in 7 subtopics.
ArrayList internals and growth
How doesArrayList store elements and grow? What is the time complexity of add at the end, add at index 0, get, and remove from the middle?You know you will add one million items to a list. How would you create it and why? What does trimToSize do?A heap dump shows millions of ArrayList objects with large empty backing arrays in a cache of small lists. How do you reduce memory?Java's ArrayList stores references to boxed objects, unlike arrays of primitives or value types in other languages. How does Project Valhalla's work on value classes relate to this, and how should you plan data-heavy code today?LinkedList
How isLinkedList implemented, and why is it rarely faster than ArrayList in practice, even for inserts?A report generator is very slow. The code is:for (int i = 0; i < list.size(); i++) {
process(list.get(i));
}
LinkedList of 200,000 items. Explain the complexity and fix.LinkedList is rarely the best choice in modern Java, yet it remains in many codebases and in the JDK itself. What does the persistence of such APIs teach about long-term platform choices, and how would you handle similar legacy choices in your own libraries?RandomAccess
What is theRandomAccess marker interface, and how do JDK algorithms such as Collections.binarySearch use it?You are writing a general-purpose list utility for a shared library. How should it handle lists that may be ArrayList, LinkedList, unmodifiable views or synchronized wrappers?SubList pitfalls
UsesubList to remove elements at indexes 10 to 19 from an ArrayList in one call, and explain why it is efficient.This code throws ConcurrentModificationException:List<Integer> page = all.subList(0, 10);
all.add(99);
System.out.println(page);
subList semantics and fix it.A cache stores list.subList(0, 100) of a huge list loaded from the database to keep only the first 100 results. Memory does not go down. Why?Many Java APIs return views, such as subList, keySet, Arrays.asList and unmodifiable wrappers, rather than copies. What is the long-term cost of view-based APIs, and when would you allow them in your organisation's shared libraries?Removing during iteration
Find the bug and fix it in two ways:for (String s : names) {
if (s.isBlank()) names.remove(s);
}
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == 0) list.remove(i);
}
list.remove(i) behave differently from list.remove(Integer.valueOf(0))?How does the fail-fast behaviour of ArrayList's iterator work, and why is ConcurrentModificationException not a reliable concurrency check?You are designing a shared event listener registry where listeners can unregister themselves while an event is being dispatched. Which list implementation and design would you choose, and why?Bugs from modifying collections while iterating keep appearing in reviews across teams. What combination of standards, tooling and API choices would you use to reduce them organisation-wide?Common list operations
GivenList<String> names, write code to: sort case-insensitively, remove blank entries, convert all to upper case in place, and find the index of "BOB". Name the List and Collections methods you use and their complexity.This code throws UnsupportedOperationException:List<Integer> ids = List.of(3, 1, 2);
Collections.sort(ids);
list.contains(null) on another List.of throws NullPointerException. Explain both and fix them.A service deduplicates incoming events with if (!seen.contains(id)) seen.add(id); where seen is an ArrayList, and CPU rises sharply as traffic grows. Explain the problem and choose better operations.Arrays.asList fixed-size view
What doesArrays.asList(new int[]{1, 2, 3}).size() return, and why? How do you get a List<Integer> from an int[]?List<String> list = Arrays.asList("a", "b");
list.add("c");
UnsupportedOperationException, but list.set(0, "z") works and also changes the original array. Explain the behaviour.A shared library method returns Arrays.asList(internalArray) to callers, and a caller's call to sort the returned list changes the library's internal state. How would you fix the API and review similar cases?No question matches these choices.