Quick revision
The short answer
A general list utility should read its input in the way that is cheap for that list, using index access only when the list implements RandomAccess and an iterator otherwise. It should never mutate its input but return a new list, unless its contract says it modifies in place, and then document that unmodifiable inputs will throw UnsupportedOperationException. It should state that it does not lock synchronized wrappers, and it should be tested against several implementations, including subList views.
What to remember
In this answer, we designed a chunk utility for any List. It reads through index access only when the list implements RandomAccess and iterates otherwise, never mutates its input and returns independent copies, documents that in-place variants throw UnsupportedOperationException on unmodifiable inputs, leaves locking of synchronized wrappers to the caller, and is tested with one parameterised suite across ArrayList, LinkedList, List.of, Arrays.asList, unmodifiable views and subList views.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A general list utility should read its input in the way that is cheap for that list, using index access only when the list implements RandomAccess and an iterator otherwise. It should never mutate its input but return a new list, unless its contract says it modifies in place, and then document that unmodifiable inputs will throw UnsupportedOperationException. It should state that it does not lock synchronized wrappers, and it should be tested against several implementations, including subList views.
Why this question is asked
Code in a shared library is called by many teams with lists we never see: ArrayLists, LinkedLists, results of List.of, views returned by subList, and Collections wrappers. A utility that works perfectly on ArrayList can be slow, surprising or unsafe on the others. Interviewers ask this design question to see whether we think about contracts as well as code. In this answer, we will design one utility, chunk(list, size), and walk through each decision: how it reads the input, whether it mutates, what it promises about immutable and synchronized lists, and how we test it.
1The Lists Our Utility Will Meet
Before designing, it helps to list what callers can pass, and what each one implies. An ArrayList has fast get(i) and supports every change. A LinkedList has slow get(i) but cheap iteration. List.of and List.copyOf return immutable lists that also reject nulls, even in contains(null). Arrays.asList returns a fixed-size view of an array: set works, add and remove throw. Collections.unmodifiableList returns a read-only view of a list that someone else may still change. Collections.synchronizedList returns a wrapper whose individual methods lock, but whose iteration must be locked by the caller. And subList returns a view into another list that fails if the backing list is structurally changed.
| Input | Fast get(i)? | Mutable? | Watch out for |
|---|---|---|---|
ArrayList | yes | yes | callers may change it later |
LinkedList | no | yes | index loops are O(n squared) |
List.of / List.copyOf | yes | no | rejects nulls, even contains(null) |
Arrays.asList | yes | set only | add and remove throw |
unmodifiableList view | as backing list | no | backing list can still change |
synchronizedList | as backing list | yes | iteration needs caller's lock |
subList view | as backing list | yes | invalid after backing list changes |
2Reading the Input Efficiently
Our chunk utility splits a list into consecutive pieces of a given size. A naive version calls get(i) in a loop, which is O(n squared) on a LinkedList. So we branch on RandomAccess, the marker interface that array-backed lists implement, and iterate otherwise.
Notice the parameter type List<? extends T>: it accepts a List<Integer> where a List<Number> result is wanted, and it tells readers we only read from it. The output is pre-sized from the input size, and the chunks are copies, not subList views, so they stay valid whatever happens to the input later. One thing to decide: List.copyOf rejects nulls. If callers may pass nulls, we return Collections.unmodifiableList of new ArrayLists instead, and document either choice.
3Never Mutating the Caller's List
The default contract for a shared utility is that it does not change its inputs. A caller who passes a list does not expect it to be sorted, filtered or emptied, and a utility that does so causes bugs far from where they appear. It also cannot work on immutable inputs at all. So chunk returns new lists and leaves the input untouched.
Some utilities exist precisely to modify in place, such as removeDuplicatesInPlace(list). Then the contract must say so in the name and the Javadoc, and must say what happens with inputs that cannot be modified: they throw UnsupportedOperationException, as the JDK's own Collections.sort does. We do not try to detect unmodifiable lists, since there is no reliable public way to ask; we let the exception surface and document it, or offer a copying variant beside the in-place one.
4Synchronized Wrappers and Thread Safety
Collections.synchronizedList makes each call lock the wrapper, but iterating it is a sequence of calls, so another thread can change the list in between. Its documentation requires callers to hold the list's lock while iterating. Our utility iterates, and it should not try to lock on behalf of the caller: it cannot know which lock protects the data, and locking on an arbitrary object can cause deadlocks.
So we document it plainly: the utility is not thread-safe with respect to its input; callers sharing a list between threads must synchronise around the call, or pass a snapshot. A caller with a synchronized list writes the code below, and a caller with a CopyOnWriteArrayList needs nothing, because its iterator works on a snapshot.
5Testing Against Many Implementations
Most bugs in list utilities only appear with one kind of list. So we write the behaviour tests once and run them over every implementation, with a JUnit parameterised test. The source of arguments includes the unusual cases: a subList view, an Arrays.asList, an unmodifiable view and an empty list.
We also add a timing guard for the LinkedList case with a few hundred thousand elements, which catches an accidental index loop, and a test that the returned chunks are unaffected by later changes to the input.
6Follow-up questions
Why not return subList views for the chunks, which would be faster?
Views are cheaper to create, but they tie the result to the input. If the caller later adds to or removes from the input list, every chunk view becomes invalid and throws ConcurrentModificationException on use, and the views keep the whole input reachable in memory. For a shared library, copies give a contract that is easy to explain. A separate, clearly named chunkViews method could offer views for callers who measure a need.
Should the utility accept null as the list argument?
Generally no. We fail fast with Objects.requireNonNull and a clear message, because a null list is almost always a bug in the caller. Accepting it and returning an empty list hides that bug. Null elements are a separate question that the contract must answer explicitly.
How should the utility handle an input that changes during the call?
It should not try. If another thread or a callback modifies the input while we iterate, ArrayList and LinkedList iterators may throw ConcurrentModificationException on a best-effort basis. Our documentation says the input must not be modified during the call, which is the same contract the JDK's own algorithms have.
7Conclusion
In this answer, we designed a chunk utility for any List. It reads through index access only when the list implements RandomAccess and iterates otherwise, never mutates its input and returns independent copies, documents that in-place variants throw UnsupportedOperationException on unmodifiable inputs, leaves locking of synchronized wrappers to the caller, and is tested with one parameterised suite across ArrayList, LinkedList, List.of, Arrays.asList, unmodifiable views and subList views.
Open with
I would start by being explicit about the contract, because a shared utility gets every kind of list. For reading, I check instanceof RandomAccess: index loops are fine on ArrayList, List.of and Arrays.asList, but on a LinkedList they are quadratic, so there I iterate.
Then, if they want more
I would not mutate the input. The utility returns new lists, and I make the results copies rather than subList views, so they stay valid if the caller changes the input later and they do not keep the whole input alive. I'd accept List of question mark extends T, since I only read from it.
If a utility exists to modify in place, the name and Javadoc say so, and they say that unmodifiable inputs such as List.of throw UnsupportedOperationException, which is how Collections.sort behaves. I don't try to detect unmodifiable lists, because there's no reliable way. For synchronized wrappers, I document that the utility does not lock.
Iteration over a synchronizedList needs the caller to hold the list's lock, and the utility can't know which lock protects the data, so the caller synchronises around the call or passes a snapshot. I also decide on null elements up front, because List.copyOf rejects them.
Finally, I test with one parameterised JUnit suite over ArrayList, LinkedList, List.of, Arrays.asList, an unmodifiable view and a subList view, checking both the result and that the input is unchanged.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You are writing a general-purpose list utility for a shared library. How should it...": https://scrivio-interview-library.netlify.app/java/lists/09a6090b22/