Java Interview Questions on ArrayList
- ArrayList is a core data structure of the Java Collections API. It combines the convenience of dynamic arrays with the power of the Collection Framework.
- For these reasons, it has become a favored interview topic to test candidates on their knowledge of data structures and performance under pressure. In this tutorial, we will provide detailed explanations of various ArrayList concepts. We'll classify questions and answers into sections based on difficulty level.
1. Basic Questions
1: What is an ArrayList?
- ArrayList is a resizable array implementation of the List interface.
- It dynamically grows/shrinks as elements are added/removed.
- Allows duplicate elements and maintains insertion order.
Code Example:
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
2: How does ArrayList differ from a standard array?
- Arrays have fixed sizes; ArrayList is resizable.
- Arrays support primitives (e.g., int[]); ArrayList only stores objects (but can use wrappers like Integer).
- ArrayList provides built-in methods (add(),remove(), etc.) and implements the List interface.
3: What is the default initial capacity of an ArrayList?
- Default capacity is 10. When exceeded, it grows by ~50% (e.g., 10 → 15 → 22, etc.).
2. Common Operations
4: How do you iterate over an ArrayList?
we can iterate over an ArrayList in four ways:
-
For-loop:
for (int i = 0; i < fruits.size(); i++) { System.out.println(fruits.get(i)); }
-
Enhanced For-loop:
for (String fruit : fruits) { System.out.println(fruit); }
-
Iterator:
Iterator<String> it = fruits.iterator(); while (it.hasNext()) { System.out.println(it.next()); }
-
Java 8+ forEach:
fruits.forEach(System.out::println);
5: How do you convert an ArrayList to an array?
String[] fruitArray = fruits.toArray(new String[0]);
6: How do you remove duplicates from an ArrayList?
Answer: Use a Set to remove duplicates:
List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 2, 3));
Set<Integer> unique = new LinkedHashSet<>(numbers);
List<Integer> uniqueList = new ArrayList<>(unique);
3. Performance & Internals
7: What is the time complexity of common ArrayList operations?
- get(int index): O(1) (direct index access).
- add(E element): Amortized O(1) (adds to end; resizing is rare).
- add(int index, E element): O(n) (shifts elements).
- remove(int index): O(n) (shifts elements).
8: How does ArrayList handle resizing?
- When the current capacity is exceeded, a new array is created with 1.5× the old capacity (e.g., 10 → 15).
- Elements are copied to the new array using Arrays.copyOf().
4. Thread Safety
9: Is ArrayList thread-safe?
- No. Concurrent modifications can cause ConcurrentModificationException.
- For thread safety, use:
- Collections.synchronizedList(new ArrayList<>()).
- CopyOnWriteArrayList (thread-safe but costly for write-heavy operations).
10: What is a ConcurrentModificationException?
- Occurs when a list is modified while being iterated.
- Solution: Use Iterator.remove() or synchronize access.
5. Advanced Topics
11: How do you sort an ArrayList?
Use Collections.sort() or Java 8+ Comparator:
Collections.sort(numbers); // Ascending order
numbers.sort((a, b) -> b - a); // Descending order
12: How do you reverse an ArrayList?
Use Collections.reverse():
Collections.reverse(numbers);
13: What is the difference between ArrayList and LinkedList?
- ArrayList: Backed by an array. Faster for random access (get()/set()).
- LinkedList: Backed by nodes. Faster for frequent insertions/deletions.
6. Coding Challenges
14: Find the intersection of two ArrayLists.
Solution:
List<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 3));
List<Integer> list2 = new ArrayList<>(Arrays.asList(2, 3, 4));
list1.retainAll(list2); // list1 becomes [2, 3]
15: Find the maximum element in an ArrayList.
Solution:
int max = Collections.max(numbers);
Or with Java 8+:
int max = numbers.stream().max(Integer::compare).get();
7. Best Practices
16: When should you use ArrayList?
Answer:
- When you need frequent read access and infrequent insertions/deletions at the end.
- Avoid using it for heavy insertions/deletions in the middle (use LinkedList instead).
17: How can you optimize ArrayList performance?
Answer:
- Pre-size it: Use new ArrayList<>(initialCapacity) if you know the expected size.
- Avoid frequent resizing: Estimate capacity upfront.
No comments