Course contents

Collections & Generics: complete interview question bank

Premium45 questions · 23 what-if follow-ups

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 questions

Collection interfaces are contracts first and data structures second, so this group checks whether you choose by operation, ordering, null policy and concurrency needs.

Question 1. DesignCoreAsked very often

How do you choose a collection when the prompt gives you only a business requirement?

Tests requirements, operations, ordering, defaults

Show answerHide answer1 follow-up

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

  1. 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.

Question 2. ConceptWarm-upCommonly asked

Why is Map not a subtype of Collection?

Tests hierarchy, contracts, entries

Show answerHide answer1 follow-up

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

  1. 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, or map.entrySet() for key-value pairs.

Question 3. ConceptSeniorSometimes asked

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

Question 4. ConceptCoreAsked very often

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

Question 5. ScenarioCoreCommonly asked

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 questions

Lists 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.

Question 6. ScenarioCoreAsked very often

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

Question 7. What does it print?Warm-upCommonly asked

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

Question 8. CodingCoreCommonly asked

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

Question 9. DebuggingCoreCommonly asked

What are the traps of subList?

Tests views, structural modification, memory retention

Model answer, how to say it: unlock with the Java Pass

Question 10. DebuggingCoreSometimes asked

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 questions

Maps are where interviews test equality contracts, hashing, ordering, atomic updates and the special-purpose implementations that solve very different problems.

Question 11. ConceptCoreAsked very often

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

Question 12. ScenarioCoreCommonly asked

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

Question 13. CodingCoreAsked very often

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

Question 14. ScenarioCoreCommonly asked

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

Question 15. DebuggingCoreAsked very often

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 questions

Sets 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.

Question 16. ScenarioWarm-upCommonly asked

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

Question 17. ConceptCoreAsked very often

How does HashSet.add(e) work internally?

Tests HashSet, HashMap backing, dummy value

Model answer, how to say it: unlock with the Java Pass

Question 18. DebuggingCoreCommonly asked

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

Question 19. CodingCoreCommonly asked

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

Question 20. ScenarioCoreSometimes asked

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 questions

Queues and deques test whether you know the access pattern: FIFO, LIFO, double-ended, priority, blocking handoff or delayed scheduling.

Question 21. ConceptWarm-upCommonly asked

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

Question 22. ScenarioWarm-upCommonly asked

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

Question 23. ConceptCoreCommonly asked

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

Question 24. What does it print?CoreCommonly asked

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

Question 25. ScenarioCoreAsked very often

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 questions

Ordering and iteration questions separate candidates who can sort a list from candidates who understand comparator contracts, cursor rules, fail-fast behavior and removal safety.

Question 26. ConceptCoreAsked very often

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

Question 27. DebuggingCoreCommonly asked

Debug this comparator: return a.priority() - b.priority();.

Tests comparator contract, overflow

Model answer, how to say it: unlock with the Java Pass

Question 28. ConceptCoreAsked very often

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

Question 29. DesignCoreCommonly asked

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

Question 30. ConceptCoreCommonly asked

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 questions

Generics questions test compile-time type safety in a runtime-erased world, especially invariance, wildcards, bounds and the few places where the compiler needs help.

Question 31. ConceptCoreAsked very often

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

Question 32. ConceptCoreAsked very often

Explain PECS with a concrete example.

Tests extends, super, producer, consumer

Model answer, how to say it: unlock with the Java Pass

Question 33. ConceptCoreAsked very often

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

Question 34. DebuggingCoreCommonly asked

Why does List<? extends Number> reject add(10)?

Tests wildcard capture, producer extends

Model answer, how to say it: unlock with the Java Pass

Question 35. DesignSeniorCommonly asked

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 questions

Utility 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.

Question 36. ConceptCoreAsked very often

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

Question 37. DebuggingCoreCommonly asked

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

Question 38. DebuggingCoreCommonly asked

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

Question 39. ConceptCoreCommonly asked

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

Question 40. CodingCoreSometimes asked

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 questions

These are the patterns I expect you to recognize in real interview problems: counting, grouping, top-K, sliding windows, stack algorithms and range queries.

Question 41. CodingCoreAsked very often

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

Question 42. DesignCoreAsked very often

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

Question 43. CodingSeniorCommonly asked

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

Question 44. ScenarioSeniorCommonly asked

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

Question 45. ScenarioCoreCommonly asked

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