📚
Collections Interview Questions
List, Set, Map, Queue implementations and internal workings.
ArrayListHashMapHashSetLinkedListConcurrent Collections
1Difference between ArrayList and LinkedList.
Easy▼
Difference between ArrayList and LinkedList.
**ArrayList**:
- Uses dynamic array implementation.
- Fast random access (get) - O(1).
- Slow manipulation (add/remove in middle) - O(n) due to shifting.
- Better for storing and accessing data.
**LinkedList**:
- Uses doubly linked list implementation.
- Slow random access - O(n).
- Fast manipulation (add/remove) - O(1) if you have the node reference.
- Better for manipulating data frequently.
2How does HashMap work internally?
Hard▼
How does HashMap work internally?
HashMap uses **hashing** to store and retrieve elements.
1. **put(K, V)**:
- Calculates hash of Key.
- Determines bucket index using `hash & (n-1)`.
- If bucket empty, stores Node.
- If collision, uses Linked List (or Red-Black Tree in Java 8+ if chain > 8) to store.
2. **get(K)**:
- Calculates hash.
- Goes to bucket.
- Traverses list/tree comparing keys using `equals()`.
Practice these concepts
Test your knowledge with our online compiler and practice problems.