Scrivio Interview library

Collections framework overview

23 questions in 8 subtopics.

Collection and Map hierarchy

ConceptFoundations6 min read
Describe the main interfaces of the Collections Framework and where Map fits. Why does Map not extend Collection?
InternalsIntermediate6 min read
What are the skeletal implementation classes such as AbstractList and AbstractMap for, and what are the optional operations of the Collection interface?
LeadershipPrincipal7 min read
The Collections Framework of 1998 chose optional operations that throw UnsupportedOperationException instead of separate read-only interfaces. Looking at how Java evolved since, what were the consequences, and what would you standardise in your own APIs?

Sequenced collections

CodingFoundations6 min read
Using Java 21 sequenced collections, get the first and last elements of a List, a LinkedHashSet and a LinkedHashMap, and iterate a list in reverse. What did this need before Java 21?
DebuggingIntermediate7 min read
After upgrading to Java 21, a project fails to compile with a conflict in a class that implements both List and Deque, or behaves differently because a custom List subclass already had a getFirst method returning a different type. Explain why.
ScenarioAdvanced7 min read
Your platform is moving from Java 17 to 21. Sequenced collections added new default methods to List, Deque, LinkedHashSet and LinkedHashMap. How do you assess the impact on internal collection implementations and libraries across teams?

Choosing a collection

ScenarioFoundations6 min read
Pick a collection for each case and justify it: unique visitor IDs, a leaderboard ordered by score, a recent-items list with fast removal of the oldest, a lookup of product by SKU that keeps insertion order.
ScenarioIntermediate7 min read
A service holds 50 million small entries in a HashMap<Long, Integer> and uses 4 GB of heap. How would you reduce memory, and what would you consider before switching implementation?
LeadershipAdvanced6 min read
Teams in your company often add third-party collection libraries such as Guava, fastutil, Eclipse Collections and Vavr. What policy do you set on collection libraries?
LeadershipPrincipal6 min read
Collection choice seems local, but over years it affects memory, cost and performance of a whole fleet. How would you make collection and data structure decisions visible and governed at company scale without micromanaging teams?

Collections utility methods

ConceptFoundations7 min read
What do these java.util.Collections methods do and what should you know about each: sort, binarySearch, unmodifiableList, synchronizedList, emptyList, nCopies, frequency, shuffle?
DebuggingIntermediate6 min read
Two threads use a Collections.synchronizedList. One iterates with for-each while the other adds, and ConcurrentModificationException appears. Why, and what are the options?

Immutable factory methods List.of and Map.of

ConceptFoundations6 min read
What are the properties of List.of, Set.of and Map.of? Why do Set.of("a", "a") and Map.of("k", 1, "k", 2) throw, and why does List.of(1, null) throw?
DebuggingIntermediate7 min read
After replacing Arrays.asList with List.of in a codebase, some calls start throwing NullPointerException from list.contains(null) and a test breaks because Set.of iteration order differs between runs. Explain both.
DesignAdvanced7 min read
You are setting coding standards for returning collections from shared library methods. Should methods return List.of copies, unmodifiable views, or mutable lists? Justify for performance and safety.
LeadershipPrincipal7 min read
The JDK added List.of and Map.of in Java 9 with strict rules: no nulls, no duplicates, unspecified iteration order. What do these choices tell you about API design for the long term, and would you apply similar rules to your company's libraries?

Converting between collections

ConceptFoundations7 min read
What does each line do, and which ones fail or surprise?
int[] nums = {3, 1, 2};
List<int[]> a = Arrays.asList(nums);
List<Integer> b = Arrays.asList(3, 1, 2);
b.add(4);
String[] arr = list.toArray(new String[0]);
List<Integer> c = Arrays.stream(nums).boxed().toList();
c.sort(null);
Set<String> s = new HashSet<>(list);
List<String> back = new ArrayList<>(s);
DebuggingIntermediate7 min read
A service converts a list of products to a map with products.stream().collect(Collectors.toMap(Product::sku, Product::discount)). It fails in production with NullPointerException although no product or sku is null. Later a colleague replaces it with a loop into a HashMap and the response order of a JSON field changes. Explain both problems.
DesignAdvanced7 min read
Profiling a high-traffic service shows that 20 percent of allocation comes from converting between collections: lists copied to sets for contains checks, streams collected to lists and immediately copied again, and defensive copies at every layer. How would you guide the team to reduce this without making the code unsafe?

Legacy collections Vector and Hashtable

ConceptFoundations6 min read
Vector, Hashtable, Stack and Enumeration are legacy collections. Explain how they differ from their modern replacements and what you would use instead in new code.
ScenarioAdvanced7 min read
A large codebase still has hundreds of uses of Vector, Hashtable and Enumeration, some in public APIs used by other teams, and some required by old APIs such as java.util.Properties and JNDI's InitialContext environment. How do you plan their removal, and where do you stop?

Pairs and tuples

CodingFoundations7 min read
Java has no general Pair or Tuple class in the JDK. A method must return both the minimum and maximum of a list. Show three ways to return two values and say which one you would choose in Java 25.
ScenarioIntermediate6 min read
In a code review you find methods returning Pair<Pair<String, Integer>, List<Pair<Long, BigDecimal>>> and maps keyed by Pair<String, String>. What do you recommend, and what do records change for map keys and grouping?