Collections & Generics: complete interview question bank
This is the Collections & Generics pass I would do before a Java backend interview. It pulls together the questions that look practical on the surface, but quickly reveal whether you understand contracts, cost, ordering, mutability and generic type safety.
Use it like a mock interview. Read the question, say your answer out loud first, then open the model answer and check whether you led with the right default, the trade-off and the edge case.
Treat this as the final pass after the topic pages: collections framework, lists, maps, sets, queues, ordering and iteration, generics, utilities and patterns.
Collections framework
5 questionsCollection interfaces are contracts first and data structures second, so this group checks whether you choose by operation, ordering, null policy and concurrency needs.
How do you choose a collection when the prompt gives you only a business requirement?
Tests requirements, operations, ordering, defaults
Show answerHide answer
Model answer
Start by translating the requirement into operations: lookup by key, preserve insertion order, sorted traversal, deduplicate, FIFO, priority, or indexed access. Then ask about mutation rate, size, nulls, thread sharing and whether order is observable by callers.
My default set is small: ArrayList for ordered indexed data, HashMap for key lookup, HashSet for uniqueness, and ArrayDeque for local queue or stack work. Move away from those when the requirement says sorted, concurrent, weak keys, reference equality, blocking, or stable access order.
How I’d say it in the interview
"I don't start with the class name. I start with the operation the code must be good at, then pick the simplest collection whose contract matches it."
Signals depth
Calling out that order has several meanings: insertion order, sorted order, access order and encounter order.
Red flags
Choosing LinkedList or TreeMap because they sound more general, without tying the choice to a real operation.
Follow-ups interviewers ask next
What if the interviewer says the collection is shared by many threads?
Then collection choice changes. You usually want a concurrent collection or external synchronization around the whole compound operation, not a normal collection plus hope.
Why is Map not a subtype of Collection?
Tests hierarchy, contracts, entries
Show answerHide answer
Model answer
A Collection<E> is a group of elements. A Map<K,V> is a group of key-value associations, and its core operations are based on keys, not element membership.
You can view a map through keySet(), values() and entrySet(), but those are views with their own contracts. Treating a map itself as a collection would make methods like add, contains and iterator ambiguous.
How I’d say it in the interview
"A collection stores elements. A map stores associations. Java gives you collection views of a map, but the map itself has a different contract."
Signals depth
Mentioning that entrySet() is the element-like view when you really need to iterate associations.
Red flags
Saying Map is unrelated because it is older or because it cannot be iterated.
Follow-ups interviewers ask next
What if I need to pass map data to an API that accepts a
Collection?Pass the right view:
map.values()for values,map.keySet()for keys, ormap.entrySet()for key-value pairs.
What did Java 21 sequenced collections add?
Tests SequencedCollection, first, last, reversed
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Why do we say HashMap lookup is expected O(1), not guaranteed O(1)?
Tests hashing, collisions, worst case
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Why are Vector, Hashtable and Stack usually weak defaults today?
Tests legacy classes, synchronization, modern alternatives
Model answer, how to say it: unlock with the Java Pass
Lists
5 questionsLists preserve positional order, so these questions focus on indexed access, shifting cost, fixed-size views, aliases and the places where LinkedList sounds better than it is.
A playlist has frequent inserts in the middle. Do you choose ArrayList or LinkedList?
Tests middle insert, locality, ListIterator
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What does list.remove(1) remove from a List<Integer>?
Tests overloads, boxing, remove by index
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
How would you remove duplicates from a list while preserving first-seen order?
Tests deduplication, LinkedHashSet, order
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What are the traps of subList?
Tests views, structural modification, memory retention
Model answer, how to say it: unlock with the Java Pass
Why can Collections.nCopies create a shared-row bug in a 2D list?
Tests aliasing, shallow copies, mutable elements
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Maps
5 questionsMaps are where interviews test equality contracts, hashing, ordering, atomic updates and the special-purpose implementations that solve very different problems.
Walk me through what happens during HashMap.put.
Tests hash, index, collisions, resize
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
A cache should drop entries when keys are no longer strongly referenced elsewhere. Which map do you use?
Tests WeakHashMap, reachability, cache semantics
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
How would you build a small LRU cache using JDK collections?
Tests LinkedHashMap, access order, eviction
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
A stock-price tool needs the closest known prices below and above a target. Which map helps?
Tests NavigableMap, floor, ceiling
Model answer, how to say it: unlock with the Java Pass
A shared counter map loses increments. What's wrong with get plus put?
Tests compound operations, ConcurrentHashMap, merge
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Sets
5 questionsSets are about uniqueness, but the real interview work is naming the equality rule, the order rule and the cost of the operation you run against the set.
You receive product names from a CSV and need each unique product once. Which set do you choose?
Tests HashSet, normalization, uniqueness
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
How does HashSet.add(e) work internally?
Tests HashSet, HashMap backing, dummy value
Model answer, how to say it: unlock with the Java Pass
A HashSet has size 1, but contains returns false after I mutate the object. Debug it.
Tests mutable keys, hashCode, equals
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Write union, intersection and difference without mutating either input.
Tests set algebra, defensive copies
Model answer, how to say it: unlock with the Java Pass
Your system has fixed roles like ADMIN, USER and AUDITOR. Which set should hold a user's roles?
Tests EnumSet, bit vectors, fixed universe
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Queues & deques
5 questionsQueues and deques test whether you know the access pattern: FIFO, LIFO, double-ended, priority, blocking handoff or delayed scheduling.
What is the difference between offer/poll and add/remove on a queue?
Tests Queue API, exceptional vs special values
Model answer, how to say it: unlock with the Java Pass
You are implementing a FIFO job scheduler in one thread. Which collection do you choose?
Tests FIFO, ArrayDeque, LinkedList
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Why is ArrayDeque usually preferred over Stack for LIFO work?
Tests Deque, Stack legacy, nulls
Model answer, how to say it: unlock with the Java Pass
What does this PriorityQueue code print?
Tests heap order, iteration order, poll
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Producers add work, consumers take work, and producers must wait when full. Which collection do you use?
Tests BlockingQueue, bounded capacity, producer-consumer
Model answer, how to say it: unlock with the Java Pass
Ordering & iteration
5 questionsOrdering and iteration questions separate candidates who can sort a list from candidates who understand comparator contracts, cursor rules, fail-fast behavior and removal safety.
When do you implement Comparable, and when do you pass a Comparator?
Tests natural order, external order, sorted collections
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Debug this comparator: return a.priority() - b.priority();.
Tests comparator contract, overflow
Model answer, how to say it: unlock with the Java Pass
What does fail-fast iteration protect you from, and what does it not protect you from?
Tests modCount, best effort, concurrency
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
removeIf or Iterator.remove: which one would you choose?
Tests removal, predicates, iterator state
Model answer, how to say it: unlock with the Java Pass
Why is an index loop over LinkedList a performance trap?
Tests LinkedList, random access, iteration cost
Model answer, how to say it: unlock with the Java Pass
Generics
5 questionsGenerics questions test compile-time type safety in a runtime-erased world, especially invariance, wildcards, bounds and the few places where the compiler needs help.
Why is List<Integer> not a List<Number>?
Tests invariance, type safety
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Explain PECS with a concrete example.
Tests extends, super, producer, consumer
Model answer, how to say it: unlock with the Java Pass
What is type erasure, and what generic information survives at runtime?
Tests erasure, casts, reflection
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Why does List<? extends Number> reject add(10)?
Tests wildcard capture, producer extends
Model answer, how to say it: unlock with the Java Pass
When would you choose a wildcard instead of a type parameter?
Tests API design, wildcards, type parameters
Model answer, how to say it: unlock with the Java Pass
Utilities & patterns
5 questionsUtility APIs look small, but they hide view-versus-copy decisions, null policies, duplicate-key behavior and the everyday transformations that show up in real services.
Compare Arrays.asList, List.of and new ArrayList<>(...).
Tests fixed-size views, immutability, nulls
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
A service throws UnsupportedOperationException on orders.add(order). How do you debug it?
Tests optional operations, unmodifiable collections
Model answer, how to say it: unlock with the Java Pass
What happens when Collectors.toMap sees duplicate keys?
Tests toMap, duplicate keys, merge function
Model answer, how to say it: unlock with the Java Pass
How do Stream.toList(), Collectors.toList() and Collectors.toUnmodifiableList() differ?
Tests streams, mutability, nulls
Model answer, how to say it: unlock with the Java Pass
Code flatten for a list of lists.
Tests nested collections, generics, streams
Model answer, how to say it: unlock with the Java Pass
Collections in action
5 questionsThese are the patterns I expect you to recognize in real interview problems: counting, grouping, top-K, sliding windows, stack algorithms and range queries.
Given a list of events, how do you count occurrences by user ID?
Tests frequency map, merge, grouping
Model answer, how to say it: unlock with the Java Pass
Find the top 10 most frequent words in a stream of words. What structure do you use?
Tests top-K, heap, frequency map
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
How would you implement a sliding-window maximum?
Tests monotonic deque, window, amortised cost
Model answer, how to say it: unlock with the Java Pass
When does a TreeMap solve a range-query problem better than a HashMap?
Tests range queries, NavigableMap, sorted keys
Model answer, how to say it: unlock with the Java Pass
How do you recognize a stack algorithm that should use ArrayDeque?
Tests stack pattern, ArrayDeque, parsing
Model answer, how to say it: 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
Topic question sets in Collections & Generics
- Collections framework: interview questions & scenarios
- Lists: interview questions & scenarios
- Maps: interview questions & scenarios
- Sets: interview questions & scenarios
- Queues & deques: interview questions & scenarios
- Ordering & iteration: interview questions & scenarios
- Generics: interview questions & scenarios
- Utilities & patterns: interview questions & scenarios
- Collections in action: interview questions & scenarios