# 01 - Collections Interview Handbook (30 Problems)

Each problem includes a concise solution. Future chapters can expand each with alternatives and tests.


## 1. ArrayList vs LinkedList

### Problem
Explain/implement this concept.

### Solution

```java
// ArrayList vs LinkedList
Comparison table; use ArrayList for random access, LinkedList for deque.
```

**Complexity / Notes:** See chapter discussion.


## 2. Vector vs ArrayList

### Problem
Explain/implement this concept.

### Solution

```java
// Vector vs ArrayList
Vector synchronized; ArrayList faster.
```

**Complexity / Notes:** See chapter discussion.


## 3. HashMap Internals

### Problem
Explain/implement this concept.

### Solution

```java
// HashMap Internals
Array+bucket+tree bins after collisions.
```

**Complexity / Notes:** See chapter discussion.


## 4. LinkedHashMap

### Problem
Explain/implement this concept.

### Solution

```java
// LinkedHashMap
Maintains insertion/access order.
```

**Complexity / Notes:** See chapter discussion.


## 5. TreeMap

### Problem
Explain/implement this concept.

### Solution

```java
// TreeMap
Red-Black Tree; O(log n).
```

**Complexity / Notes:** See chapter discussion.


## 6. HashSet

### Problem
Explain/implement this concept.

### Solution

```java
// HashSet
Backed by HashMap.
```

**Complexity / Notes:** See chapter discussion.


## 7. LinkedHashSet

### Problem
Explain/implement this concept.

### Solution

```java
// LinkedHashSet
Ordered HashSet.
```

**Complexity / Notes:** See chapter discussion.


## 8. TreeSet

### Problem
Explain/implement this concept.

### Solution

```java
// TreeSet
Sorted unique elements.
```

**Complexity / Notes:** See chapter discussion.


## 9. PriorityQueue

### Problem
Explain/implement this concept.

### Solution

```java
// PriorityQueue
Binary heap.
```

**Complexity / Notes:** See chapter discussion.


## 10. ArrayDeque

### Problem
Explain/implement this concept.

### Solution

```java
// ArrayDeque
Preferred stack/queue.
```

**Complexity / Notes:** See chapter discussion.


## 11. Queue vs Deque

### Problem
Explain/implement this concept.

### Solution

```java
// Queue vs Deque
FIFO vs double-ended.
```

**Complexity / Notes:** See chapter discussion.


## 12. Comparable vs Comparator

### Problem
Explain/implement this concept.

### Solution

```java
// Comparable vs Comparator
Natural vs custom ordering.
```

**Complexity / Notes:** See chapter discussion.


## 13. Collections.sort

### Problem
Explain/implement this concept.

### Solution

```java
// Collections.sort
TimSort for objects.
```

**Complexity / Notes:** See chapter discussion.


## 14. Binary Search

### Problem
Explain/implement this concept.

### Solution

```java
// Binary Search
Collections.binarySearch on sorted list.
```

**Complexity / Notes:** See chapter discussion.


## 15. Reverse HashMap

### Problem
Explain/implement this concept.

### Solution

```java
public static <K,V> Map<V,K> reverse(Map<K,V> input){
    Map<V,K> out=new HashMap<>();
    for(var e:input.entrySet()){
        out.put(e.getValue(),e.getKey());
    }
    return out;
}
```

**Complexity / Notes:** See chapter discussion.


## 16. Merge Maps

### Problem
Explain/implement this concept.

### Solution

```java
// Merge Maps
Map.merge().
```

**Complexity / Notes:** See chapter discussion.


## 17. Frequency Counter

### Problem
Explain/implement this concept.

### Solution

```java
Map<String,Integer> freq=new HashMap<>();
for(String s:list){
    freq.merge(s,1,Integer::sum);
}
```

**Complexity / Notes:** See chapter discussion.


## 18. Remove Duplicates

### Problem
Explain/implement this concept.

### Solution

```java
List<Integer> unique=new ArrayList<>(new LinkedHashSet<>(list));
```

**Complexity / Notes:** See chapter discussion.


## 19. Top K Elements

### Problem
Explain/implement this concept.

### Solution

```java
// Top K Elements
PriorityQueue min-heap.
```

**Complexity / Notes:** See chapter discussion.


## 20. LRU Cache

### Problem
Explain/implement this concept.

### Solution

```java
Map<Integer,String> cache=new LinkedHashMap<>(16,0.75f,true){
 protected boolean removeEldestEntry(Map.Entry<Integer,String> e){
   return size()>100;
 }
};
```

**Complexity / Notes:** See chapter discussion.


## 21. WeakHashMap

### Problem
Explain/implement this concept.

### Solution

```java
// WeakHashMap
GC removes weak keys.
```

**Complexity / Notes:** See chapter discussion.


## 22. IdentityHashMap

### Problem
Explain/implement this concept.

### Solution

```java
// IdentityHashMap
Uses == instead of equals().
```

**Complexity / Notes:** See chapter discussion.


## 23. EnumMap

### Problem
Explain/implement this concept.

### Solution

```java
// EnumMap
Fast enum-key map.
```

**Complexity / Notes:** See chapter discussion.


## 24. EnumSet

### Problem
Explain/implement this concept.

### Solution

```java
// EnumSet
Bit-vector enum set.
```

**Complexity / Notes:** See chapter discussion.


## 25. CopyOnWriteArrayList

### Problem
Explain/implement this concept.

### Solution

```java
// CopyOnWriteArrayList
Read-heavy concurrent list.
```

**Complexity / Notes:** See chapter discussion.


## 26. ConcurrentHashMap

### Problem
Explain/implement this concept.

### Solution

```java
ConcurrentHashMap<String,Integer> map=new ConcurrentHashMap<>();
map.compute("A",(k,v)->v==null?1:v+1);
```

**Complexity / Notes:** See chapter discussion.


## 27. BlockingQueue

### Problem
Explain/implement this concept.

### Solution

```java
BlockingQueue<String> q=new LinkedBlockingQueue<>();
q.put("A");
System.out.println(q.take());
```

**Complexity / Notes:** See chapter discussion.


## 28. Synchronized Collection

### Problem
Explain/implement this concept.

### Solution

```java
// Synchronized Collection
Collections.synchronizedList().
```

**Complexity / Notes:** See chapter discussion.


## 29. Unmodifiable Collections

### Problem
Explain/implement this concept.

### Solution

```java
// Unmodifiable Collections
List.copyOf()/Collections.unmodifiableList().
```

**Complexity / Notes:** See chapter discussion.


## 30. Fail-fast Iterator

### Problem
Explain/implement this concept.

### Solution

```java
// Fail-fast Iterator
ConcurrentModificationException.
```

**Complexity / Notes:** See chapter discussion.
