Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

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.

We use names.removeIf(String::isBlank) to drop blanks in one O(n) pass, names.replaceAll(s -> s.toUpperCase(Locale.ROOT)) to convert in place, names.sort(String.CASE_INSENSITIVE_ORDER) for a stable O(n log n) TimSort, and names.indexOf("BOB") for an O(n) linear search with equals. Collections.binarySearch would be O(log n), but only on a list sorted by the same comparator. All of these change the list, so they throw UnsupportedOperationException on immutable lists such as List.of.

Why this question is asked

Day-to-day list work is mostly these four operations: filter, transform, sort and search. Java 8 added methods on List and Collection that do them in place in one line, and many developers still write manual loops with subtle bugs. Interviewers ask this coding question to see whether we know the modern API, the cost of each call, and the small traps such as locale-sensitive upper-casing and binary search on the wrong order. In this answer, we will write the whole task, then look at each method: what it does, how it does it, and its complexity, and finish with what happens on immutable lists.

1The Whole Task in Five Lines

Here is the full solution on a sample list. We remove blanks first, because there is no reason to upper-case or sort entries we are about to throw away.

Java
List<String> names = new ArrayList<>(List.of("bob", " ", "alice", "", "Carol"));
names.removeIf(String::isBlank); // [bob, alice, Carol]
names.replaceAll(s -> s.toUpperCase(Locale.ROOT)); // [BOB, ALICE, CAROL]
names.sort(String.CASE_INSENSITIVE_ORDER); // [ALICE, BOB, CAROL]
int i = names.indexOf("BOB"); // 1
int j = Collections.binarySearch(names, "bob", String.CASE_INSENSITIVE_ORDER); // 1
Methods used and their cost on an ArrayList
StepMethodCost
Remove blanksremoveIf(String::isBlank)O(n), one pass
Upper-case in placereplaceAll(UnaryOperator)O(n)
Sort ignoring casesort(CASE_INSENSITIVE_ORDER)O(n log n), stable
Find "BOB"indexOf(Object)O(n), uses equals
Find on a sorted listCollections.binarySearchO(log n), same comparator

Notice that the list is created with new ArrayList<>(List.of(...)). List.of alone would be immutable, and every mutating call below would fail. The order in the question, sort first, would also work, but filtering first means less to sort and transform. Once everything is upper case, the case-insensitive sort gives the same order as natural ordering, but sorting case-insensitively is still the safe statement of intent if the transform step changes later.

2RemoveIf: Filtering in One Pass

removeIf takes a predicate, a function that returns true for elements to remove. String::isBlank, available since Java 11, returns true for empty strings and strings of only whitespace, unlike isEmpty, which is true only for "". ArrayList implements removeIf in two phases: it scans the array once to mark the matching positions, then moves the survivors forward in a single compaction pass and nulls out the tail. So it is O(n) no matter how many elements match, and it cannot hit ConcurrentModificationException the way removing inside a for-each loop does. It returns true if anything was removed.

3ReplaceAll: Transforming In Place

replaceAll applies a UnaryOperator, a function from an element to an element of the same type, to each position and stores the result back. It is O(n) and allocates no new list, unlike a stream that collects a new one.

Java
List<String> t = new ArrayList<>(List.of("istanbul"));
t.replaceAll(String::toUpperCase); // depends on the default locale
System.out.println("istanbul".toUpperCase(Locale.forLanguageTag("tr"))); // İSTANBUL with a dotted capital I
t.replaceAll(s -> s.toUpperCase(Locale.ROOT)); // [ISTANBUL] on every machine

There is a trap here. toUpperCase() with no argument uses the JVM's default locale, and in a Turkish locale the letter i becomes a dotted capital I, a different character, so "bob" and "BOB" style comparisons can fail on some servers. For identifiers and data rather than user-facing text, we pass Locale.ROOT. String::toUpperCase would compile, but the explicit lambda makes the locale choice visible.

4Sort: Stable TimSort

list.sort(comparator) sorts in place. Collections.sort(list, comparator) simply calls it. ArrayList sorts its internal array directly with TimSort, a merge sort variant that is stable, meaning elements that compare equal keep their original relative order, and runs in O(n log n) in the worst case. It is also adaptive: on input that is already sorted, or made of a few sorted runs, it approaches O(n).

String.CASE_INSENSITIVE_ORDER compares strings character by character after case folding, so "alice", "Bob" and "carol" sort alphabetically regardless of capitals. Passing null to sort uses natural ordering, which for strings puts all upper-case letters before lower-case ones. Stability matters when sorting by several keys: sort by the secondary key first, then by the primary one, or better, build one comparator with Comparator.comparing(...).thenComparing(...).

Java
List<String> mixed = new ArrayList<>(List.of("bob", "Alice", "carol", "Bob"));
mixed.sort(null); // [Alice, Bob, bob, carol]: natural order, capitals first
mixed.sort(String.CASE_INSENSITIVE_ORDER); // [Alice, Bob, bob, carol]: "Bob" and "bob" equal, order kept

5IndexOf Versus binarySearch

indexOf(o) walks from the start and returns the first index where o.equals(element) is true, or -1. It is O(n) and works on any list, sorted or not. Because it uses equals, it is case-sensitive: indexOf("bob") returns -1 on our upper-cased list.

Collections.binarySearch is O(log n), but only correct on a list sorted by the same order we search with. Our list was sorted with CASE_INSENSITIVE_ORDER, so we must pass that comparator; searching with natural order on a list sorted case-insensitively can return a wrong answer. When the key is missing, binarySearch returns a negative number, minus the insertion point minus one, which tells us where the key would go.

Java
names.indexOf("BOB"); // 1
names.indexOf("bob"); // -1: equals is case-sensitive
Collections.binarySearch(names, "bob", String.CASE_INSENSITIVE_ORDER); // 1
Collections.binarySearch(names, "BRIAN", String.CASE_INSENSITIVE_ORDER); // -3: would be inserted at index 2

For a single lookup, sorting just to binary search is not worth it, since the sort costs O(n log n). Binary search pays off when we sort once and search many times; and for many lookups by key, a HashMap or HashSet is usually better still.

6On Immutable Lists, All of This Throws

removeIf, replaceAll and sort modify the list, so on an immutable list such as List.of(...), List.copyOf(...) or the result of Stream.toList() they throw UnsupportedOperationException. On Arrays.asList, a fixed-size list, replaceAll and sort work because they only set elements, but removeIf throws if it finds something to remove. When the input may be immutable, we copy it first, new ArrayList<>(input), or produce a new list with a stream.

Java
List<String> fixed = List.of("bob", " ");
fixed.removeIf(String::isBlank); // UnsupportedOperationException
List<String> clean = fixed.stream()
.filter(s -> !s.isBlank())
.map(s -> s.toUpperCase(Locale.ROOT))
.sorted(String.CASE_INSENSITIVE_ORDER)
.toList(); // [BOB], a new immutable list

7Follow-up questions

Is removeIf faster than using an Iterator with remove()?

On an ArrayList, yes, when many elements match. Each Iterator.remove() shifts the tail of the array, so removing k elements costs about k times n, while removeIf compacts once in O(n). On a LinkedList the two are similar, since unlinking a node is cheap either way.

Why is the sort stable, and when does that matter?

TimSort is a merge sort, and merging takes from the left run on ties, which preserves the original order of equal elements. It matters when the list was already ordered by another key, such as date, and we sort by name: people with the same name stay in date order.

Should we use a stream or the in-place methods?

In-place methods are best when we own a mutable list and want to change it, with no extra allocation. Streams are best when the input must not be changed or may be immutable, because they build a new list. Both are correct; the choice is about ownership.

8Conclusion

In this answer, we filtered, transformed, sorted and searched a list with the modern List methods. removeIf removes in one O(n) pass, replaceAll transforms in place and should use Locale.ROOT for upper-casing data, sort runs a stable O(n log n) TimSort, indexOf is an O(n) search with equals, and binarySearch is O(log n) only with the same comparator the list was sorted by. All the mutating calls throw UnsupportedOperationException on immutable lists, where a stream producing a new list is the alternative.

What to say in the interviewAbout 90 seconds aloud

Open with

I would make sure the list is mutable, say new ArrayList of the input, and then do it in four calls. First names.removeIf(String::isBlank), because there is no point transforming or sorting entries I will drop. isBlank, since Java 11, covers empty and whitespace-only strings, and ArrayList's removeIf marks matches in one scan and compacts once, so it is O(n).

Then, if they want more

Then names.replaceAll with s.toUpperCase(Locale.ROOT), which writes each result back in place, O(n). I pass Locale.ROOT because the no-argument toUpperCase uses the default locale, and in Turkish the letter i upper-cases to a dotted capital I. Then names.sort(String.CASE_INSENSITIVE_ORDER). Collections.sort delegates to this.

On ArrayList it sorts the internal array with TimSort, a stable merge sort, O(n log n) worst case and close to linear on nearly sorted data. Finally names.indexOf of BOB, which is a linear search with equals, O(n), and case-sensitive.

Since the list is sorted, Collections.binarySearch with the same CASE_INSENSITIVE_ORDER comparator would be O(log n), but only if I pass the same comparator the list was sorted with; otherwise results are undefined. And a missing key gives minus insertion point minus one.

One caveat: removeIf, replaceAll and sort all mutate, so on List.of, List.copyOf or a Stream.toList result they throw UnsupportedOperationException. There I would use a stream with filter, map and sorted into a new list.

About this answer

Last updated
12 November 2026
Versions it names
Java 11, Java 8
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.
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 "Given List<String> names, write code to: sort case-insensitively, remove blank entries...": https://scrivio-interview-library.netlify.app/java/lists/2df3aaa09a/