Section 4 Β· 30 questions

Collections

30 interview questions and answers in the Collections section.

English Collections Source Markdown
Language versions: English Russian Ukrainian

Questions in this section

  1. What are the main Collection Framework interfaces?
  2. What is the difference between List, Set and Queue?
  3. What is the difference between ArrayList and LinkedList?
  4. When to use ArrayList vs LinkedList?
  5. What is the time complexity of operations in ArrayList?
  6. What is the time complexity of operations in LinkedList?
  7. What is Vector and how does it differ from ArrayList?
  8. What is Stack?
  9. What is Queue and what implementations exist?
  10. What is Deque?
  11. What is the difference between HashSet, LinkedHashSet and TreeSet?
  12. How does HashSet work internally?
  13. What is TreeSet and how does it work?
  14. What is Map and what implementations exist?
  15. What is the difference between HashMap, LinkedHashMap and TreeMap?
  16. When to use TreeMap?
  17. What is WeakHashMap?
  18. What is ConcurrentHashMap?
  19. How does ConcurrentHashMap ensure thread-safety?
  20. What is CopyOnWriteArrayList?
  21. When to use synchronized collections?
  22. How to get a synchronized collection?
  23. What is Collections.unmodifiableList()?
  24. How does Collections.unmodifiableList() work internally?
  25. What is the difference between Iterator and ListIterator?
  26. What are fail-fast and fail-safe iterators?
  27. What is ConcurrentModificationException?
  28. How to properly remove elements during iteration?
  29. What is Comparable and Comparator?
  30. What operations does the Collection interface support?

Study navigator

30 questions for Middle Java Developer interview preparation.


πŸ“‹ All Questions

# Question Difficulty Level
1 What are the main Collection Framework interfaces ⭐⭐
2 What is the difference between List, Set and Queue ⭐
3 What is the difference between ArrayList and LinkedList ⭐⭐
4 When to use ArrayList vs LinkedList ⭐⭐
5 What is the time complexity of operations in ArrayList ⭐⭐⭐
6 What is the time complexity of operations in LinkedList ⭐⭐⭐
7 What is Vector and how does it differ from ArrayList ⭐
8 What is Stack ⭐
9 What is Queue and what implementations exist ⭐⭐
10 What is Deque ⭐⭐
11 What is the difference between HashSet, LinkedHashSet and TreeSet ⭐⭐
12 How does HashSet work internally ⭐⭐⭐
13 What is TreeSet and how does it work ⭐⭐
14 What is Map and what implementations exist ⭐⭐
15 What is the difference between HashMap, LinkedHashMap and TreeMap ⭐⭐
16 When to use TreeMap ⭐⭐⭐
17 What is WeakHashMap ⭐⭐⭐
18 What is ConcurrentHashMap ⭐⭐⭐
19 How does ConcurrentHashMap ensure thread-safety ⭐⭐⭐
20 What is CopyOnWriteArrayList ⭐⭐⭐
21 When to use synchronized collections ⭐⭐
22 How to get a synchronized collection ⭐⭐
23 What is Collections.unmodifiableList() ⭐
24 How does Collections.unmodifiableList() work internally ⭐⭐
25 What is the difference between Iterator and ListIterator ⭐⭐
26 What are fail-fast and fail-safe iterators ⭐⭐
27 What is ConcurrentModificationException ⭐⭐
28 How to properly remove elements during iteration ⭐⭐
29 What is Comparable and Comparator ⭐⭐
31 What operations does the Collection interface support ⭐⭐

πŸ—ΊοΈ Topic Dependency Map

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   1. Collection Framework (Overview)      β”‚
                    β”‚   Collection, List, Set, Queue, Map       β”‚
                    β””β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚          β”‚          β”‚
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β”‚          └──────────┐
            β–Ό                     β–Ό                     β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ LIST (3-10)   β”‚    β”‚ SET (11-13)   β”‚    β”‚ MAP (14-20)        β”‚
    β”‚ 3. ArrayList  β”‚    β”‚ 11. HashSet   β”‚    β”‚ 14. Map overview   β”‚
    β”‚    vs Linked  β”‚    β”‚     vs Linked β”‚    β”‚ 15. HashMap vs     β”‚
    β”‚ 4. When what  β”‚    β”‚     vs Tree   β”‚    β”‚     LinkedHashMap  β”‚
    β”‚ 5-6. Big O    β”‚    β”‚ 12. HashSet   β”‚    β”‚     vs TreeMap     β”‚
    β”‚ 7. Vector     β”‚    β”‚     internals β”‚    β”‚ 16. When TreeMap   β”‚
    β”‚ 8. Stack      β”‚    β”‚ 13. TreeSet   β”‚    β”‚ 17. WeakHashMap    β”‚
    β”‚ 9. Queue      β”‚    β”‚               β”‚    β”‚ 18-19. CHM         β”‚
    β”‚ 10. Deque     β”‚    β”‚               β”‚    β”‚ 20. CopyOnWrite    β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚                    β”‚                     β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   THREAD-SAFETY (18-22)                  β”‚
                    β”‚   18-19. ConcurrentHashMap               β”‚
                    β”‚   20. CopyOnWriteArrayList               β”‚
                    β”‚   21-22. synchronized collections        β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   ITERATORS and REMOVAL (23-28)          β”‚
                    β”‚   23-24. unmodifiableList                β”‚
                    β”‚   25. Iterator vs ListIterator           β”‚
                    β”‚   26. fail-fast vs fail-safe             β”‚
                    β”‚   27. ConcurrentModificationException    β”‚
                    β”‚   28. Proper removal                     β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   COMPARISON and COMMON OPS (29, 31)     β”‚
                    β”‚   29. Comparable vs Comparator           β”‚
                    β”‚   31. Collection operations              β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🟒 Junior Level (weeks 1-2)

Step Topic Files Goal
1 Collection Framework Overview Q1, Q2 Interface hierarchy, List vs Set vs Queue
2 List: ArrayList vs LinkedList Q3, Q4 When to use what
3 Vector, Stack, Queue, Deque Q7, Q8, Q9, Q10 Legacy and modern analogs
4 Set: HashSet vs TreeSet Q11, Q13 Uniqueness and sorting
5 Map: HashMap vs TreeMap Q14, Q15, Q16 When to use what
6 unmodifiableList Q23, Q24 Defensive copying
7 Iterator vs ListIterator Q25 Iteration and removal

🟑 Middle Level (weeks 3-4)

Step Topic Files Goal
1 Big O ArrayList Q5 Amortization, resize, System.arraycopy
2 Big O LinkedList Q6 Cache miss, GC pressure
3 HashSet internals Q12 hashCode, equals, treeification
4 WeakHashMap Q17 WeakReference, GC, ReferenceQueue
5 synchronized collections Q21, Q22 Decorator, mutex, check-then-act
6 fail-fast/fail-safe Q26 modCount, CME, snapshot
7 CME Q27 Causes, subList trap, views
8 Removal during iteration Q28 removeIf, Iterator.remove
9 Comparable vs Comparator Q29 Timsort, overflow, transitivity
10 Collection operations Q31 bulk ops, parallelStream, Spliterator

πŸ”΄ Senior Level (weeks 5-6)

Step Topic Files Goal
1 ArrayList internals Q5 (Senior) SIMD, branch prediction, CPU pipeline
2 LinkedList internals Q6 (Senior) Cache locality catastrophe, pipeline stall
3 CHM thread-safety Q18, Q19 CAS, volatile, synchronized, LongAdder
4 CopyOnWriteArrayList Q20 Fail-safe, stale data, eventual consistency
5 HashSet internals Q12 (Senior) Treeification, transient, hash collisions
6 TreeMap internals Q13, Q16 Red-black tree, rebalancing, Skip List
7 unmodifiableList internals Q24 Defensive copy, RandomAccess, GC pressure
8 Concurrent collections Q21 (Senior) High contention, compound operations

πŸ”— Key Connections Between Topics

Topic: List

Q3 (ArrayList vs LinkedList) β†’ Q4 (When to use what) β†’ Q5-Q6 (Big O)
                                                ↓
                                       Q7 (Vector) β†’ Q8 (Stack) β†’ Q9 (Queue) β†’ Q10 (Deque)

Key connections:

  • Q3 ↔ Q5-Q6: Big O explains why ArrayList is faster than LinkedList in practice
  • Q4 ↔ Q28: removeIf() solves the removal-in-loop problem
  • Q7 ↔ Q18: Vector β†’ ConcurrentHashMap (evolution of thread-safety)
  • Q9-Q10 ↔ Q18: BlockingQueue, ConcurrentLinkedQueue as alternatives

Topic: Set

Q11 (HashSet vs LinkedHashSet vs TreeSet) β†’ Q12 (HashSet internals) β†’ Q13 (TreeSet)

Key connections:

  • Q11 ↔ Q14: Set uses Map internally (HashSet = HashMap<E, Object>)
  • Q12 ↔ Q15: HashSet internals = HashMap internals
  • Q13 ↔ Q16: TreeSet = TreeMap.keySet(), when TreeMap is needed

Topic: Map

Q14 (Map overview) β†’ Q15 (HashMap vs LinkedHashMap vs TreeMap) β†’ Q16 (TreeMap)
     ↓                          ↓                          ↓
Q17 (WeakHashMap)      Q12 (HashSet internals)    Q18-19 (ConcurrentHashMap)
                                ↓
                       Q20 (CopyOnWriteArrayList)

Key connections:

  • Q14 ↔ Q1: Map is technically NOT a Collection, but part of the Framework
  • Q15 ↔ Q12: HashMap internals = HashSet internals
  • Q17 ↔ Q3 (Memory and GC): WeakReference, GC, SoftReference
  • Q18-19 ↔ Q21: CHM vs synchronizedList β€” different approaches to thread-safety
  • Q18 ↔ Q20: CHM for mixed workload, COWAL for read-heavy

Topic: Thread-Safety

Q7 (Vector) β†’ Q21 (synchronized collections) β†’ Q22 (How to get)
     ↓                          ↓                          ↓
Q18 (CHM) ←────────────── Q19 (CHM thread-safety)  Q20 (CopyOnWriteArrayList)

Key connections:

  • Q7 ↔ Q21: Vector β€” legacy approach, synchronizedList β€” modern
  • Q18 ↔ Q20: CHM = granular locks, COWAL = snapshot
  • Q21 ↔ Q26: synchronizedList = fail-fast, CHM = weakly consistent

Topic: Iterators and removal

Q25 (Iterator vs ListIterator) β†’ Q26 (fail-fast vs fail-safe)
     ↓                                  ↓
Q27 (CME) ←──────────────────────── Q28 (Proper removal)
     ↑
Q23-24 (unmodifiableList)

Key connections:

  • Q25 ↔ Q28: Iterator.remove() β€” the correct way to remove
  • Q26 ↔ Q27: fail-fast throws CME, fail-safe β€” does not
  • Q23-24 ↔ Q27: unmodifiableList does not protect against CME through view

πŸŽ“ Cheat Sheet: What to Know for Each Level

🟒 Junior

  • Hierarchy: Collection β†’ List/Set/Queue, Map β€” in parallel
  • ArrayList vs LinkedList: ArrayList for most cases
  • HashSet = uniqueness, TreeSet = sorting
  • HashMap = key-value, TreeMap = sorted by key
  • Vector/Stack β€” legacy, use ArrayList/ArrayDeque
  • Iterator for iteration, cannot remove in for-each
  • unmodifiableList = defensive wrapper, not deep immutability

🟑 Middle

  • Big O: ArrayList add = amort. O(1), get = O(1); LinkedList get = O(n)
  • Cache locality: ArrayList > LinkedList 10-100x on iteration
  • HashSet = HashMap internally, treeification at > 8 elements per bucket
  • ConcurrentHashMap: CAS + volatile + synchronized, does NOT block reads
  • CopyOnWriteArrayList: snapshot, fail-safe, O(n) copy on write
  • fail-fast = modCount, CME = bug detector (not synchronization!)
  • removeIf() = O(n) for ArrayList, Iterator.remove() = O(nΒ²)
  • Comparable = natural order, Comparator = external sorting

πŸ”΄ Senior

  • System.arraycopy: JVM intrinsic, SIMD, branch prediction
  • LinkedList cache miss: each node β€” a separate object on the heap
  • CHM: CounterCells (LongAdder), ForwardingNode, ReservationNode
  • WeakHashMap: ReferenceQueue, expungeStaleEntries, Minor GC
  • ConcurrentHashMap size(): not a lock, but a counter via CAS
  • Timsort: stable sorting, transitivity, overflow
  • Spliterator: characteristics (SIZED, DISTINCT, SORTED) for Stream optimization
  • Java 21: SequencedCollection/SequencedSet/SequencedMap

πŸ“ Format of Each File

Each file contains:

  • 🟒 Junior Level β€” basic understanding, simple analogies, examples
  • 🟑 Middle Level β€” internals, typical mistakes, practical examples
  • πŸ”΄ Senior Level β€” deep dive, edge cases, production experience, monitoring
  • 🎯 Interview Cheat Sheet β€” key points, frequent questions, red flags, related topics