Quick revision
The short answer
We fix it in stages. In the next patch release, every public method returns a defensive copy (array.clone()) instead of the internal array, which keeps the signatures and binary compatibility and stops the corruption at once. Then we add new methods that return unmodifiable List views, deprecate the array methods with a migration note, and remove them only in a major version after usage scans show callers have moved, with tests that prove a returned array cannot change the library's state.
What to remember
In this answer, we fixed a library that leaked its internal arrays without breaking anyone. A return type change would break binary compatibility because the descriptor includes the return type, so the first release only returns defensive copies, after checking that the copy cost is small on hot paths. Then we add List-returning methods, deprecate the array methods with forRemoval and a migration note, remove them in a major version only after scans show callers have moved, and keep tests and a build rule that prove a returned array can never change the library's state again.
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.
We fix it in stages. In the next patch release, every public method returns a defensive copy (array.clone()) instead of the internal array, which keeps the signatures and binary compatibility and stops the corruption at once. Then we add new methods that return unmodifiable List views, deprecate the array methods with a migration note, and remove them only in a major version after usage scans show callers have moved, with tests that prove a returned array cannot change the library's state.
Why this question is asked
A shared library is a contract with many teams at once, and the contract includes compiled code we cannot see. Changing a return type from String[] to List<String> looks like a small refactoring in our repository, but every service compiled against the old version would fail at run time with NoSuchMethodError until it is rebuilt. Interviewers ask this at a senior level because the answer has to balance an urgent correctness fix against compatibility and the pace at which other teams can move. In this answer, we will recap why a return type change breaks binary compatibility, apply the immediate fix, check its cost, introduce the new API, retire the old one safely, and lock it all in with tests.
1A Quick Recap: Why Changing the Return Type Breaks Callers
When javac compiles a call such as config.allowedHosts(), it writes into the caller's class file a method reference that includes the full method descriptor: the name, the parameter types and the return type. A method returning String[] has a different descriptor from one returning List<String>.
At run time, the JVM links each call by looking for a method with exactly that descriptor. If we ship a new library version where allowedHosts() returns a List, an old caller still asks for the String[] version, finds nothing, and fails with NoSuchMethodError. This is binary incompatibility: the source might even compile again after small edits, but already-built services break the moment they pick up our new jar, often through a transitive dependency they did not choose to upgrade.
So the constraint in the question is real. Whatever we do in a minor release must keep every existing method with its exact signature.
2Return Defensive Copies From the Existing Methods
The corruption happens because callers receive the library's own array and modify it. The quickest safe fix keeps the method exactly as it is from the outside and changes only what it returns.
Notice that callers do not need to change anything or even recompile. Any caller that only read the array behaves exactly as before. The only callers that see a difference are those that wrote into the array and expected the library to notice, and those callers were relying on the very bug we are fixing, so the release note should say so plainly.
We apply the same change to every array-returning method, and also to any method that stores an array passed in by a caller, because that is the same leak in the other direction.
3Measure the Cost of Copying
A defensive copy allocates on every call, so before shipping we check where these methods are called and how big the arrays are. Most arrays returned by a configuration or metadata library hold a handful of elements, and copying ten references costs a few nanoseconds, far below anything a service would notice.
The cases to look at are hot paths with large arrays, for example a method returning a 100,000 element lookup table called on every request. For those we measure with JMH, and with an allocation profile from JFR on a service that uses the library heavily. If the copy really is too expensive, that method is the first candidate for the read-only List or wrapper in the next step, and we can tell its heavy callers to cache the result rather than calling it per request.
4Add List-Returning Methods and Deprecate the Old Ones
Copies stop the corruption, but the API still invites misuse and allocates on every call. In the same or the next minor release, we add new methods that return an unmodifiable List, which callers cannot modify at all and which we can build once and share.
The new methods need new names, because Java cannot overload on return type alone. The @Deprecated annotation with forRemoval = true makes the compiler warn every caller that still uses the old method, and the Javadoc tells them exactly what to use instead. Sharing one immutable List is cheaper than copying an array per call, so the new path is both safer and faster, which makes migration an easy sell.
5Remove the Old Methods in a Major Version
Removing a public method is a breaking change, so it belongs in a major version, 3.0 in our example, and only after we know callers have moved. We do not guess this. We scan: a code search across the organisation's repositories for calls to allowedHosts(), or a bytecode scan of deployed artifacts, which also catches services nobody has touched in a year. Tools such as jdeprscan, which ships with the JDK, report uses of deprecated APIs in compiled jars.
When the scan shows a few remaining callers, we contact those teams or send them a change. Only when usage is near zero do we cut the major version, and we announce the removal with a date. The timeline might be two or three minor releases over six months, which is enough for most teams to pick up the change during normal work.
- 2.5: return clonessame signatures, corruption stops
- Measure hot pathsJMH and JFR allocation profiles
- 2.6: add List methodsdeprecate arrays with
forRemoval - Scan callerscode search and jdeprscan on jars
- Usage near zero?Yes3.0: remove array methodsNoHelp remaining teams migrate
- Tests guard every releasea returned value cannot change state
6 steps in order. Play them, or step through them one at a time.
6Tests That Prove the Fix
Finally, we make the fix permanent with tests, so a later refactoring cannot bring the leak back. For every method that returns or accepts an array, a test changes the array and checks that the library's state did not change.
We can also add an architecture rule, for example with ArchUnit, that fails the build if a new public method in the library returns an array type, so the pattern does not come back in new code.
7Follow-up questions
Why not return an unmodifiable List view of the array from the existing method?
Because the existing method's return type is String[], and a List is not an array. Any change to the declared return type changes the method descriptor and breaks compiled callers. The clone is the only change that keeps the descriptor and still protects the state.
What if some callers depended on modifying the returned array?
Then they were changing the library's internal state on purpose, which was never part of the contract. The release note should call this out, and if such a use is legitimate, the library should offer an explicit method for it, such as withAllowedHosts(...) returning a new configuration.
Is changing a return type to a subtype safe?
Not in binary terms either. Even a narrower return type changes the descriptor, so old callers would fail to link. javac can generate bridge methods for overriding in subclasses, but that does not help when we change the method in place.
8Conclusion
In this answer, we fixed a library that leaked its internal arrays without breaking anyone. A return type change would break binary compatibility because the descriptor includes the return type, so the first release only returns defensive copies, after checking that the copy cost is small on hot paths. Then we add List-returning methods, deprecate the array methods with forRemoval and a migration note, remove them in a major version only after scans show callers have moved, and keep tests and a build rule that prove a returned array can never change the library's state again.
Open with
I would split this into an urgent fix and a migration. The urgent fix goes in the next patch release: every method that returns an internal array returns array.clone() instead, and any constructor or setter that stores a caller's array copies it.
Then, if they want more
That keeps every method descriptor identical, so already-compiled services keep linking, and it stops the corruption immediately. Before shipping, I would check the cost, because each call now allocates. Most of these arrays are small, so it is nanoseconds, but for any large array on a hot path I would measure with JMH and a JFR allocation profile.
Then, in a minor release, I would add new methods with new names that return unmodifiable Lists, built once and shared, and mark the array methods @Deprecated with forRemoval and a Javadoc note saying what to use. I can't just change the return type, because the descriptor includes it and old callers would get NoSuchMethodError.
Removal waits for a major version, and only after a code search and a jdeprscan over deployed jars show usage is close to zero. Throughout, I would add tests that modify a returned array and assert the library state did not change, plus an ArchUnit rule against new array-returning public methods.
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 Java Language Specification, Java SE 21, Java SE 21 API 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 "A shared library returns internal arrays from many public methods, and several services...": https://scrivio-interview-library.netlify.app/java/arrays/3715fdf577/