Course contents

Collections framework: interview questions & scenarios

Premium12 questions · 15 what-if follow-ups

These are the framework-level collection questions I ask when I want to know whether you choose data structures from requirements, not from class names. Answer it out loud first, then open the model answer, and use the linked lessons when a follow-up exposes a gap: The collections hierarchy, Choosing the right collection and Big-O cheat sheet for collections.

Question 1. ScenarioWarm-upAsked very often

You are building a library system and need to look up books by ISBN. Which collection would you use, and what would you say about its cost?

Tests key lookup, HashMap, expected O(1)

Show answerHide answer2 follow-ups

Model answer

Use a HashMap<String, Book>, with the ISBN as the key and the Book as the value. The operation the requirement makes important is exact lookup by a unique key, so a list scan is the wrong shape.

The honest cost is expected O(1) for get, put and remove, assuming a good hashCode and equals contract for the key. A resize can make one insert O(n), and collision-heavy bins are protected by tree bins in Java 8+, but the interview answer should not flatten that into "HashMap is always O(1)".

record Book(String isbn, String title) { }

Map<String, Book> byIsbn = new HashMap<>();
byIsbn.put("978-0134685991", new Book("978-0134685991", "Effective Java"));
Book book = byIsbn.get("978-0134685991");

This lines up with the decision path in Choosing the right collection: key lookup starts with HashMap, then moves only if ordering, enum keys, reference identity or concurrency changes the requirement.

How I’d say it in the interview

"I'd use HashMap<String, Book>. ISBN is the lookup key, so I want map lookup rather than scanning a list. I'd call it expected constant time, with the normal hash caveats: resize, collisions and correct equals/hashCode."

Signals depth

Saying expected O(1), and knowing that iteration over a sparse HashMap can cost more than size because it scans buckets too.

Red flags

"Use an ArrayList and loop until the ISBN matches" when lookup is the main operation. Also: "HashMap is O(1) in every case."

Follow-ups interviewers ask next

  1. What if the books must print sorted by ISBN?

    Use TreeMap<String, Book> if sorted iteration and range navigation are part of the core workflow. If sorting is only for one report, keep the HashMap and sort a copy of the keys at the edge.

  2. What if the ISBN object is mutable?

    Do not use mutable key fields. If a key's equality or hash code changes while it is in a hash map, the entry can become unreachable through normal lookup.

Question 2. DesignCoreAsked very often

Multiple threads update a shared collection of counters. Which collection do you choose, and why are Hashtable or Collections.synchronizedMap weaker answers?

Tests ConcurrentHashMap, atomic operations, synchronized wrappers

Show answerHide answer2 follow-ups

Model answer

For a shared mutable map, start with ConcurrentHashMap<K,V>, then use its atomic methods such as merge, compute or putIfAbsent. If the values are hot counters, a common shape is ConcurrentHashMap<String, LongAdder>.

Hashtable and Collections.synchronizedMap(new HashMap<>()) synchronize individual method calls. That is better than a plain HashMap under mutation, but it does not make a multi-step operation atomic unless you hold the same external lock around the whole sequence. They also serialize more access through one monitor, while ConcurrentHashMap is designed for concurrent reads and updates.

ConcurrentMap<String, Integer> hits = new ConcurrentHashMap<>();
hits.merge("/checkout", 1, Integer::sum);

The requirement is not "make any collection synchronized". It is "keep a shared invariant correct under interleaving threads". Sometimes that still means an explicit lock or a different design, especially when the invariant spans several collections.

How I’d say it in the interview

"I'd use ConcurrentHashMap and its atomic update methods. A synchronized wrapper or Hashtable locks method by method, so if absent then put is still not one atomic action unless I add my own lock."

Signals depth

Mentioning that ConcurrentHashMap rejects null keys and values so get(key) == null can mean absent without ambiguity.

Red flags

"Use Hashtable because it is thread-safe" with no discussion of compound actions.

Follow-ups interviewers ask next

  1. What if I need to update two maps together?

    A thread-safe collection is not enough. Use one explicit lock around both maps, or redesign so one map owns the combined state and one atomic compute updates it.

  2. What if reads are thousands of times more common than writes on a small listener list?

    That is a CopyOnWriteArrayList shape, not a map shape. Reads iterate a stable snapshot, while each write copies the array.

Question 3. ScenarioCoreCommonly asked

A task manager must process tasks by priority rather than insertion order. Which collection fits, and when does the answer change?

Tests PriorityQueue, comparator, blocking priority queue

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

Question 4. ScenarioCoreAsked very often

Producers add work, consumers take work, and producers must wait when the queue is full. Which collection would you use?

Tests BlockingQueue, bounded queues, producer-consumer

Model answer, how to say it, 2 follow-ups: unlock with the Java Pass

Question 5. ScenarioWarm-upCommonly asked

The prompt says: "I need random access by index, and order doesn't matter." What collection do you choose?

Tests ArrayList, requirement contradiction, index implies order

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

Question 6. ScenarioCoreCommonly asked

A leaderboard maps scores to player names and must be sorted ascending by score. What collection do you use?

Tests TreeMap, duplicate scores, comparator tie-breakers

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

Question 7. ConceptWarm-upAsked very often

Why is Map not a Collection in Java?

Tests hierarchy, map views, entries

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

Question 8. ConceptWarm-upAsked very often

What is the difference between Collection and Collections?

Tests interface vs utility class, static helpers

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

Question 9. ConceptCoreCommonly asked

What did Java 21 sequenced collections add, and why does it matter in interviews?

Tests SequencedCollection, SequencedSet, SequencedMap, reversed views

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

Question 10. ConceptCoreCommonly asked

How do you talk about Vector, Hashtable and Stack in modern Java?

Tests legacy classes, per-method synchronization, modern defaults

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

Question 11. ConceptCoreAsked very often

Why do interviewers say HashMap.get is expected O(1), not guaranteed O(1)?

Tests expected complexity, collisions, resize, tree bins

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

Question 12. ConceptCoreAsked very often

Why is ArrayList.add(e) amortised O(1) if growing the array is O(n)?

Tests amortised complexity, growth, capacity

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 Collections framework