Scrivio Interview library

Iteration semantics

16 questions in 4 subtopics.

Iterator and ListIterator

ConceptFoundations6 min read
What can a ListIterator do that an Iterator cannot, and where is its cursor positioned?
CodingFoundations5 min read
This code throws ConcurrentModificationException:
for (String s : names) {
if (s.isBlank()) names.remove(s);
}
where names is an ArrayList. Show two correct ways to remove blank names.
DesignIntermediate7 min read
You want to expose a repository method that returns all rows of a 50-million-row table for export without loading them into memory. Compare returning an Iterator, an Iterable, and a Stream, considering resource cleanup.
DesignAdvanced6 min read
A platform library offers an Iterator<Record> over paginated results from a remote API for many services. Teams report missing or duplicated records when data changes during iteration, and leaked HTTP connections when they stop early. Design the iteration contract.
ConceptPrincipal7 min read
Iterator has no close method and separate hasNext and next calls, while Spliterator uses a single tryAdvance and reports characteristics, and BaseStream implements AutoCloseable. What lessons do these design differences teach for long-lived iteration APIs over remote or resource-backed data?

Fail-fast and fail-safe

ConceptFoundations6 min read
Explain fail-fast iterators and the alternative behaviour of iterators from CopyOnWriteArrayList and ConcurrentHashMap.
ScenarioIntermediate7 min read
A reporting endpoint iterates a ConcurrentHashMap of 2 million account balances to compute a total while transfers update balances concurrently. Finance reports totals that do not match. Explain and propose a fix.
DesignAdvanced7 min read
A shared library used by 30 services exposes getSubscribers() returning its internal ArrayList while a background thread updates it. Services get intermittent ConcurrentModificationException and occasional wrong results. You own the library. What contract do you define and how do you change it without breaking users?
LeadershipPrincipal6 min read
Fail-fast iterators detect misuse early but only on a best-effort basis, while weakly consistent iterators never fail but may miss updates. What broader principle on failing fast versus degrading would you set for your platform's libraries and services, and where is weak consistency acceptable?

ConcurrentModificationException

DebuggingFoundations6 min read
A single-threaded event bus iterates its listeners list and calls each listener; one listener unregisters itself during the callback. The application throws ConcurrentModificationException even though there is only one thread. Explain and fix.
DebuggingFoundations5 min read
Why does this code NOT throw ConcurrentModificationException, and why is it still a bug?
List<String> l = new ArrayList<>(List.of("a", "b", "c"));
for (String s : l) {
if (s.equals("b")) l.remove(s);
}
System.out.println(l);
DebuggingIntermediate6 min read
In production a scheduled job that iterates a shared ArrayList occasionally fails with ConcurrentModificationException while request threads add to the list. A teammate proposes catching the exception and retrying. What do you say?
ScenarioAdvanced7 min read
After a shared library upgrade, 15 services start throwing ConcurrentModificationException at a low rate in production. You are leading the incident. Describe your actions during the incident and afterwards.

ForEach and removeIf

ConceptFoundations7 min read
Compare Iterable.forEach with an enhanced for loop, and explain why ArrayList.removeIf is better than removing in a loop.
DebuggingIntermediate6 min read
Code iterates a Collections.synchronizedList with an enhanced for loop and occasionally throws ConcurrentModificationException, but list.forEach(...) never does. Explain, and show how to iterate correctly.
ConceptPrincipal7 min read
Java moved from Enumeration (1.0) to fail-fast Iterator (1.2) to internal iteration with forEach, Spliterator and streams (8), and added Stream Gatherers in Java 24. Why did each step happen, and what does internal iteration let the platform do that external iteration could not?