Java collections interview quiz (5 questions)
May 30, 2026
- Java
- Collections
- Backend
These questions are written in the style many interviewers use: a small scenario, a concrete trade-off, and one best default choice. Work through them before an interview loop or use them as a warm-up before timed practice.
Once you want questions tied to a real job description instead of general Java prep, use PrepAhead to generate a role-specific set in the app.
Question 1
You are reviewing backend code that stores ordered results and frequently calls get(i). Which List implementation is the best default choice?
Reveal answer
Answer
ArrayList - ArrayList is the best default choice when you need fast random access by index.
Explanation
ArrayList stores elements in a resizable array, so indexed reads are O(1). LinkedList must walk nodes for get(i), which is O(n). CopyOnWriteArrayList is specialized for many reads and very few writes. Vector is legacy and synchronized by default.
Example
List<String> candidates = new ArrayList<>();
candidates.add("alice");
String first = candidates.get(0); Common interview notes
A common follow-up is when LinkedList is actually useful. Mention queue-like workloads or frequent inserts/removes at known ends, not random access.
Question 2
Your API response must always return customer names sorted alphabetically by key. Which Map implementation fits that requirement best?
Reveal answer
Answer
TreeMap - TreeMap is the right choice when keys must stay sorted.
Explanation
TreeMap keeps keys ordered by natural ordering or a provided Comparator. HashMap gives no iteration-order guarantee. LinkedHashMap preserves insertion or access order, not sorted order. ConcurrentHashMap is for thread-safe access, not sorted iteration.
Example
Map<String, Integer> scores = new TreeMap<>();
scores.put("zoe", 7);
scores.put("anna", 9);
// Iteration order: anna, zoe Common interview notes
Interviewers often ask you to compare TreeMap O(log n) operations with HashMap average O(1) access.
Question 3
A service needs to deduplicate user IDs and answer contains(id) checks quickly. Which collection is the best default fit?
Reveal answer
Answer
HashSet - HashSet is the best default choice for deduplication plus fast membership checks.
Explanation
HashSet is designed for near-constant-time add, remove, and contains when hash codes are well distributed. ArrayList and LinkedList require linear scans for contains. TreeSet keeps elements sorted, which adds ordering cost you do not need here.
Common interview notes
Be ready to explain the equals/hashCode contract and why mutable keys are dangerous in hash-based collections.
Question 4
You want predictable iteration order that matches insertion order for a Map, but you do not need the keys sorted. Which implementation should you pick?
Reveal answer
Answer
LinkedHashMap - LinkedHashMap preserves insertion order while keeping the Map API.
Explanation
LinkedHashMap adds ordering on top of hash-based lookup. HashMap does not guarantee iteration order. TreeMap sorts by key rather than preserving insertion order. Hashtable is legacy and not the modern default choice.
Common interview notes
A strong answer also mentions that LinkedHashMap can be configured for access-order iteration, which is useful in simple LRU-style caches.
Question 5
During an interview, you are asked what happens if one thread updates a ConcurrentHashMap while another thread is iterating it. Which answer is most accurate?
Reveal answer
Answer
The iterator is weakly consistent and may reflect some concurrent updates - ConcurrentHashMap iterators are weakly consistent and may reflect some, all, or none of the concurrent updates.
Explanation
Unlike fail-fast iterators on many non-concurrent collections, ConcurrentHashMap does not throw ConcurrentModificationException on every concurrent write. It also does not take a full snapshot for each read. The design favors concurrency over a perfectly fixed iteration view.
Common interview notes
A useful follow-up is when you would still prefer explicit locking or a snapshot copy for deterministic reads.
Full answer key
Review every answer in one place, with explanations and interview tips below.
1. You are reviewing backend code that stores ordered results and frequently calls get(i). Which List implementation is the best default choice?
Answer summary
ArrayList — ArrayList is the best default choice when you need fast random access by index.
ArrayList stores elements in a resizable array, so indexed reads are O(1). LinkedList must walk nodes for get(i), which is O(n). CopyOnWriteArrayList is specialized for many reads and very few writes. Vector is legacy and synchronized by default.
List<String> candidates = new ArrayList<>();
candidates.add("alice");
String first = candidates.get(0); Interview tip: A common follow-up is when LinkedList is actually useful. Mention queue-like workloads or frequent inserts/removes at known ends, not random access.
2. Your API response must always return customer names sorted alphabetically by key. Which Map implementation fits that requirement best?
Answer summary
TreeMap — TreeMap is the right choice when keys must stay sorted.
TreeMap keeps keys ordered by natural ordering or a provided Comparator. HashMap gives no iteration-order guarantee. LinkedHashMap preserves insertion or access order, not sorted order. ConcurrentHashMap is for thread-safe access, not sorted iteration.
Map<String, Integer> scores = new TreeMap<>();
scores.put("zoe", 7);
scores.put("anna", 9);
// Iteration order: anna, zoe Interview tip: Interviewers often ask you to compare TreeMap O(log n) operations with HashMap average O(1) access.
3. A service needs to deduplicate user IDs and answer contains(id) checks quickly. Which collection is the best default fit?
Answer summary
HashSet — HashSet is the best default choice for deduplication plus fast membership checks.
HashSet is designed for near-constant-time add, remove, and contains when hash codes are well distributed. ArrayList and LinkedList require linear scans for contains. TreeSet keeps elements sorted, which adds ordering cost you do not need here.
Interview tip: Be ready to explain the equals/hashCode contract and why mutable keys are dangerous in hash-based collections.
4. You want predictable iteration order that matches insertion order for a Map, but you do not need the keys sorted. Which implementation should you pick?
Answer summary
LinkedHashMap — LinkedHashMap preserves insertion order while keeping the Map API.
LinkedHashMap adds ordering on top of hash-based lookup. HashMap does not guarantee iteration order. TreeMap sorts by key rather than preserving insertion order. Hashtable is legacy and not the modern default choice.
Interview tip: A strong answer also mentions that LinkedHashMap can be configured for access-order iteration, which is useful in simple LRU-style caches.
5. During an interview, you are asked what happens if one thread updates a ConcurrentHashMap while another thread is iterating it. Which answer is most accurate?
Answer summary
The iterator is weakly consistent and may reflect some concurrent updates — ConcurrentHashMap iterators are weakly consistent and may reflect some, all, or none of the concurrent updates.
Unlike fail-fast iterators on many non-concurrent collections, ConcurrentHashMap does not throw ConcurrentModificationException on every concurrent write. It also does not take a full snapshot for each read. The design favors concurrency over a perfectly fixed iteration view.
Interview tip: A useful follow-up is when you would still prefer explicit locking or a snapshot copy for deterministic reads.
Try each question on your own, then open "Reveal answer" for the full write-up. For tailored practice and Check feedback on open-ended responses, use PrepAhead in the app - not on blog pages.