Collections interview questions
List, Set, Map implementations and their trade-offs.
158 questions with answers and explanations · 155 flashcards
Key ideas
ArrayList: O(1) random access, amortized O(1) append.LinkedList: O(n) indexed access.HashMap: O(1) average get/put; one null key allowed.TreeMap: sorted, O(log n).List.remove(int)removes by index;remove(Object)removes by value. Watch out withList<Integer>.Comparabledefines natural order;Comparatordefines alternative orders.
Questions (page 3 of 4)
- Which of the following is true about ArrayLists?Multiple choiceEasy
- Given the following ArrayList operations, what will be the state of the list after execution? List<String> list = new ArrayList<>(); list.add("Apple");…Multiple choiceHard
- Why might ArrayDeque be preferred over Stack for LIFO operations?Multiple choiceHard
- Why can using a LinkedList for implementing a queue become a problem in practice?Multiple choiceHard
- Which method of the Iterator interface can potentially throw a NoSuchElementException?Multiple choiceMedium
- In Java, which collection should you use if you need fast lookup and do not care about the order of elements?Multiple choiceMedium
- How would you explain a potential issue when using ArrayDeque for stack operations in a multi-threaded context in an interview?Multiple choiceHard
- Which operation would you use to access the front element of a Queue without removing it?Multiple choiceEasy
- Two Product objects compare equal by SKU, but their hashCode() values differ. What bug is likely in HashSet or HashMap usage?Multiple choiceHard
- Why can using a HashMap with complex keys become a problem in practice?Multiple choiceHard
- When iterating a Map with entrySet(), which entry method allows modification of a value?Multiple choiceHard
- Which approach is explicitly designed for safe removal while traversing a collection?Multiple choiceHard
- Which of the following operations is not supported by the Set interface?Multiple choiceMedium
- After iterator removal, what remains in the fruit list?Multiple choiceMedium
- Why can using an enhanced for loop over an iterator when traversing a collection become a problem in production code?Multiple choiceMedium
- How does a HashSet ensure all elements are unique when storing objects?Multiple choiceHard
- What might cause a HashMap to degrade to O(n) performance for get() operations?Multiple choiceHard
- Given a HashSet<Integer> with elements {1, 2, 3}, what will be the result of calling contains(2)?Multiple choiceMedium
- When should you prefer a TreeSet over a HashSet for storing product names?Multiple choiceMedium
- Which of these operations would typically have the worst performance on an ArrayList?Multiple choiceMedium
- Which of the following correctly describes the behavior of the List interface in Java?Multiple choiceEasy
- Why might you prefer a LinkedHashMap over a HashMap in certain scenarios?Multiple choiceHard
- What does the following code snippet print? List<String> items = Arrays.asList("A", "B", "C"); for (String item : items) { if (item.equals("B"))…Multiple choiceMedium
- How would you explain the expected time complexity of get() and put() operations in a HashMap in an interview?Multiple choiceHard
- Why might a HashSet not prevent duplicates as expected when using custom objects?Multiple choiceHard
- Which collection is the clearest fit for tracking distinct coupon codes that have already been applied?Multiple choiceEasy
- Which Java collection is best suited for storing unique product IDs with fast lookup?Multiple choiceEasy
- Which collection is the best fit if you need to maintain a sorted catalog of product prices in an e-commerce application?Multiple choiceMedium
- What happens if you try to remove an element from a List while iterating over it using an enhanced for loop?Multiple choiceMedium
- Which of the following collections provides the fastest iteration when the order of elements is not important?Multiple choiceMedium
- Why do teams use an enhanced for loop over a traditional for loop in production code?Multiple choiceEasy
- In terms of performance, why might you choose an ArrayList over a LinkedList for an in-memory product catalog?Multiple choiceHard
- Given a HashSet<String> with elements {"apple", "banana", "cherry"}, what will be the result of adding a new String object with value "banana"?Multiple choiceHard
- You have a HashSet storing custom Product objects. Why might duplicate Products be inadvertently added?Multiple choiceMedium
- Which Java collection guarantees no duplicates and provides high-performance operations for adding and checking elements?Multiple choiceEasy
- Why does calling list.clear() on an ArrayList matter in practice?Multiple choiceMedium
- Which of the following is a valid reason to use a TreeMap over a HashMap?Multiple choiceMedium
- Why can using HashSet with mutable objects as elements become a problem in production code?Multiple choiceHard
- Why must Product implement consistent equals and hashCode before being stored in a HashSet?Multiple choiceMedium
- You need a collection for product categories that allows null elements and quick random access. What would you use?Multiple choiceHard
- Which method is used to check if a HashSet contains a specific object?Multiple choiceEasy
- Which of the following statements about HashMap is true?Multiple choiceMedium
- Which of the following is a feature of using ArrayDeque over LinkedList for queue operations?Multiple choiceEasy
- What happens if you use a mutable object as a key in a HashMap and modify it after insertion?Multiple choiceHard
- Which Java collection class is best suited for implementing a queue that supports both FIFO and LIFO operations?Multiple choiceEasy
- How do you create an ArrayList with an initial capacity of 10?Multiple choiceEasy
- Given a Deque, what is the result of calling addFirst() followed by pollLast()?Multiple choiceMedium
- Why do teams use an enhanced for loop over a basic for loop in Java in production code?Multiple choiceEasy
- Which collection allows you to store key-value pairs where quick lookup of product details by ID is crucial?Multiple choiceMedium
- Given the following code, what is the output? Deque<String> deque = new ArrayDeque<>(); deque.addFirst("A"); deque.addLast("B"); deque.addFirst("C");…Multiple choiceHard