Scrivio Interview library

Lists

24 questions in 7 subtopics.

ArrayList internals and growth

InternalsFoundations6 min read
How does ArrayList store elements and grow? What is the time complexity of add at the end, add at index 0, get, and remove from the middle?
CodingFoundations7 min read
You know you will add one million items to a list. How would you create it and why? What does trimToSize do?
ScenarioIntermediate6 min read
A heap dump shows millions of ArrayList objects with large empty backing arrays in a cache of small lists. How do you reduce memory?
LeadershipPrincipal6 min read
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

ConceptFoundations6 min read
How is LinkedList implemented, and why is it rarely faster than ArrayList in practice, even for inserts?
DebuggingIntermediate6 min read
A report generator is very slow. The code is:
for (int i = 0; i < list.size(); i++) {
process(list.get(i));
}
where list is a LinkedList of 200,000 items. Explain the complexity and fix.
LeadershipPrincipal6 min read
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

ConceptIntermediate6 min read
What is the RandomAccess marker interface, and how do JDK algorithms such as Collections.binarySearch use it?
DesignAdvanced7 min read
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

CodingFoundations6 min read
Use subList to remove elements at indexes 10 to 19 from an ArrayList in one call, and explain why it is efficient.
DebuggingFoundations6 min read
This code throws ConcurrentModificationException:
List<Integer> page = all.subList(0, 10);
all.add(99);
System.out.println(page);
Explain subList semantics and fix it.
ScenarioIntermediate6 min read
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?
LeadershipPrincipal6 min read
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

CodingFoundations6 min read
Find the bug and fix it in two ways:
for (String s : names) {
if (s.isBlank()) names.remove(s);
}
DebuggingFoundations6 min read
This index loop does not remove all zeros:
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == 0) list.remove(i);
}
Also, why does list.remove(i) behave differently from list.remove(Integer.valueOf(0))?
InternalsIntermediate6 min read
How does the fail-fast behaviour of ArrayList's iterator work, and why is ConcurrentModificationException not a reliable concurrency check?
DesignAdvanced7 min read
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?
LeadershipAdvanced6 min read
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

CodingFoundations7 min read
Given List<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.
DebuggingFoundations6 min read
This code throws UnsupportedOperationException:
List<Integer> ids = List.of(3, 1, 2);
Collections.sort(ids);
and list.contains(null) on another List.of throws NullPointerException. Explain both and fix them.
ScenarioIntermediate7 min read
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

CodingFoundations6 min read
What does Arrays.asList(new int[]{1, 2, 3}).size() return, and why? How do you get a List<Integer> from an int[]?
DebuggingFoundations7 min read
List<String> list = Arrays.asList("a", "b");
list.add("c");
throws UnsupportedOperationException, but list.set(0, "z") works and also changes the original array. Explain the behaviour.
ScenarioIntermediate7 min read
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?