Skip to content
Sharp

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 with List<Integer>.
  • Comparable defines natural order; Comparator defines alternative orders.

Questions (page 3 of 4)

  1. Which of the following is true about ArrayLists?Multiple choiceEasy
  2. 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
  3. Why might ArrayDeque be preferred over Stack for LIFO operations?Multiple choiceHard
  4. Why can using a LinkedList for implementing a queue become a problem in practice?Multiple choiceHard
  5. Which method of the Iterator interface can potentially throw a NoSuchElementException?Multiple choiceMedium
  6. In Java, which collection should you use if you need fast lookup and do not care about the order of elements?Multiple choiceMedium
  7. How would you explain a potential issue when using ArrayDeque for stack operations in a multi-threaded context in an interview?Multiple choiceHard
  8. Which operation would you use to access the front element of a Queue without removing it?Multiple choiceEasy
  9. Two Product objects compare equal by SKU, but their hashCode() values differ. What bug is likely in HashSet or HashMap usage?Multiple choiceHard
  10. Why can using a HashMap with complex keys become a problem in practice?Multiple choiceHard
  11. When iterating a Map with entrySet(), which entry method allows modification of a value?Multiple choiceHard
  12. Which approach is explicitly designed for safe removal while traversing a collection?Multiple choiceHard
  13. Which of the following operations is not supported by the Set interface?Multiple choiceMedium
  14. After iterator removal, what remains in the fruit list?Multiple choiceMedium
  15. Why can using an enhanced for loop over an iterator when traversing a collection become a problem in production code?Multiple choiceMedium
  16. How does a HashSet ensure all elements are unique when storing objects?Multiple choiceHard
  17. What might cause a HashMap to degrade to O(n) performance for get() operations?Multiple choiceHard
  18. Given a HashSet<Integer> with elements {1, 2, 3}, what will be the result of calling contains(2)?Multiple choiceMedium
  19. When should you prefer a TreeSet over a HashSet for storing product names?Multiple choiceMedium
  20. Which of these operations would typically have the worst performance on an ArrayList?Multiple choiceMedium
  21. Which of the following correctly describes the behavior of the List interface in Java?Multiple choiceEasy
  22. Why might you prefer a LinkedHashMap over a HashMap in certain scenarios?Multiple choiceHard
  23. 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
  24. How would you explain the expected time complexity of get() and put() operations in a HashMap in an interview?Multiple choiceHard
  25. Why might a HashSet not prevent duplicates as expected when using custom objects?Multiple choiceHard
  26. Which collection is the clearest fit for tracking distinct coupon codes that have already been applied?Multiple choiceEasy
  27. Which Java collection is best suited for storing unique product IDs with fast lookup?Multiple choiceEasy
  28. Which collection is the best fit if you need to maintain a sorted catalog of product prices in an e-commerce application?Multiple choiceMedium
  29. What happens if you try to remove an element from a List while iterating over it using an enhanced for loop?Multiple choiceMedium
  30. Which of the following collections provides the fastest iteration when the order of elements is not important?Multiple choiceMedium
  31. Why do teams use an enhanced for loop over a traditional for loop in production code?Multiple choiceEasy
  32. In terms of performance, why might you choose an ArrayList over a LinkedList for an in-memory product catalog?Multiple choiceHard
  33. 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
  34. You have a HashSet storing custom Product objects. Why might duplicate Products be inadvertently added?Multiple choiceMedium
  35. Which Java collection guarantees no duplicates and provides high-performance operations for adding and checking elements?Multiple choiceEasy
  36. Why does calling list.clear() on an ArrayList matter in practice?Multiple choiceMedium
  37. Which of the following is a valid reason to use a TreeMap over a HashMap?Multiple choiceMedium
  38. Why can using HashSet with mutable objects as elements become a problem in production code?Multiple choiceHard
  39. Why must Product implement consistent equals and hashCode before being stored in a HashSet?Multiple choiceMedium
  40. You need a collection for product categories that allows null elements and quick random access. What would you use?Multiple choiceHard
  41. Which method is used to check if a HashSet contains a specific object?Multiple choiceEasy
  42. Which of the following statements about HashMap is true?Multiple choiceMedium
  43. Which of the following is a feature of using ArrayDeque over LinkedList for queue operations?Multiple choiceEasy
  44. What happens if you use a mutable object as a key in a HashMap and modify it after insertion?Multiple choiceHard
  45. Which Java collection class is best suited for implementing a queue that supports both FIFO and LIFO operations?Multiple choiceEasy
  46. How do you create an ArrayList with an initial capacity of 10?Multiple choiceEasy
  47. Given a Deque, what is the result of calling addFirst() followed by pollLast()?Multiple choiceMedium
  48. Why do teams use an enhanced for loop over a basic for loop in Java in production code?Multiple choiceEasy
  49. Which collection allows you to store key-value pairs where quick lookup of product details by ID is crucial?Multiple choiceMedium
  50. Given the following code, what is the output? Deque<String> deque = new ArrayDeque<>(); deque.addFirst("A"); deque.addLast("B"); deque.addFirst("C");…Multiple choiceHard