Course contents

Lists: interview questions & scenarios

Premium12 questions · 13 what-if follow-ups

These are the list questions I use to test whether you can see the array, nodes, views and overloads behind small Java snippets. Answer it out loud first, then open the model answer, and revisit the lessons when a detail feels fuzzy: How ArrayList works, How LinkedList works, ArrayList vs LinkedList, Everyday List operations and List algorithms.

Question 1. ConceptCoreAsked very often

How does an ArrayList grow, and what is the default capacity of a new empty list?

Tests lazy capacity, growth factor, amortised add

Show answerHide answer1 follow-up

Model answer

A no-argument new ArrayList<>() does not immediately allocate a 10-element array. It points at a shared empty array. On the first add, it allocates the default capacity, which is 10 in OpenJDK. When that backing array fills, JDK 21 grows to roughly old + (old >> 1), about 1.5x, and copies references with Arrays.copyOf.

size is the number of actual elements. Capacity is the length of the internal array and is not normally visible. add(e) is amortised O(1): most calls write one slot, while occasional growth calls copy the current contents.

List<String> tags = new ArrayList<>(); // shared empty backing array
tags.add("new");                      // first real array, capacity 10
tags.add("sale");                     // size 2

If you know the count, use new ArrayList<>(expectedSize) or ensureCapacity before a large batch. Do not cargo-cult trimToSize; it can save memory after a spike, but it also removes spare capacity and makes the next append more likely to grow.

How I’d say it in the interview

"An ArrayList is an Object array plus size. The no-arg constructor is lazy; first add creates capacity 10. When full, it grows by about 1.5x and copies references, so append is amortised constant time, not guaranteed constant time for every single call."

Signals depth

Mentioning that growth copies references, not deep copies of elements.

Red flags

"ArrayList starts with size 10" or "it doubles every time" as a Java 21 claim.

Follow-ups interviewers ask next

  1. What if I construct new ArrayList<>(0)?

    It starts with an empty backing array and grows from zero when needed. The important interview point is still that size and capacity are different.

Question 2. ScenarioCoreAsked very often

A playlist has frequent additions and deletions in the middle. Should you use ArrayList or LinkedList?

Tests ListIterator, middle edits, cache locality

Show answerHide answer2 follow-ups

Model answer

The nuanced answer is: LinkedList only wins if you already hold the current position, usually through a ListIterator, and perform several edits at that cursor. Then insertion and removal are pointer rewires after the node is known.

If each operation starts with an index, a song ID or a search, LinkedList first walks to the position. That is O(n), plus node allocation and pointer chasing. ArrayList shifts a contiguous suffix with System.arraycopy, which is often faster in practice even though the Big-O table says O(n).

LinkedList<String> playlist = new LinkedList<>(List.of("intro", "ad", "song"));
ListIterator<String> it = playlist.listIterator();
while (it.hasNext()) {
    if (it.next().equals("ad")) {
        it.remove();       // O(1) at cursor
        it.add("station"); // O(1) at cursor
    }
}

So I default to ArrayList for lists. I choose LinkedList for a cursor-edit workflow I can describe clearly, not for the vague phrase "middle inserts".

How I’d say it in the interview

"If the playlist UI already has a cursor at the current song and edits around it, LinkedList can make sense. If we look up by index or song ID each time, the search dominates, and ArrayList is often faster because shifting an array is cache-friendly."

Signals depth

Separating "relink at known node" from "find the node".

Red flags

"LinkedList, because middle insert is O(1)" without naming how the middle is found.

Follow-ups interviewers ask next

  1. What if it is a FIFO play queue?

    Use ArrayDeque for normal queue behavior. A playlist list and a queue are different access patterns.

  2. What if the playlist is small, say under 200 songs?

    Pick the simpler representation, usually ArrayList, unless the API naturally exposes a ListIterator. Constant factors and simplicity dominate at that size.

Question 3. What does it print?CoreAsked very often

What does this print, and why does remove(1) not remove the value 1?

Tests remove(int), remove(Object), List<Integer> overload

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 4. What does it print?Warm-upAsked very often

What happens when you call add on a list returned by Arrays.asList?

Tests fixed-size list, backing array, UnsupportedOperationException

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 5. CodingCoreAsked very often

Remove duplicates from a list while preserving first-seen order.

Tests LinkedHashSet, stream distinct, equality

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 6. ScenarioCoreAsked very often

Two lists need an intersection. What is the complexity trap?

Tests retainAll, HashSet membership, multiset semantics

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 7. CodingCoreAsked very often

Reverse a singly linked list. Write the code and explain the invariant.

Tests pointer reversal, O(1) space, invariant

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 8. DebuggingCoreCommonly asked

What are the pitfalls of subList?

Tests backed view, ConcurrentModificationException, copying

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 9. DebuggingCoreCommonly asked

Why does Collections.nCopies create the shared-row bug in a 2D list?

Tests repeated references, matrix initialization

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 10. DebuggingWarm-upCommonly asked

Why does Collections.copy(dest, src) throw IndexOutOfBoundsException here?

Tests destination size, overwrite semantics

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 11. What does it print?SeniorSometimes asked

What does this print with List.of, stream().toList() and a mutable copy?

Tests unmodifiable lists, Java 16 toList, mutable copy

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 12. ScenarioSeniorCommonly asked

You need list difference, but duplicates matter. Why is removeAll wrong?

Tests multiset difference, removeAll semantics, frequency map

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Premium question set

Keep reading with the Java Pass

The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.

From ₹199 · one-time payment · No auto-renewal

Lessons in Lists