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 2 of 4)

  1. In a HashMap, what happens if you put() a new value for an existing key?Multiple choiceMedium
  2. For storing a list of unique user IDs where insertion order must be preserved, which collection is most appropriate?Multiple choiceMedium
  3. What will be the output when attempting to add a duplicate element to a HashSet?Multiple choiceMedium
  4. Which of the following statements about the Iterator interface is true?Multiple choiceMedium
  5. How does the ArrayDeque handle the addition of elements when the underlying array is full?Multiple choiceHard
  6. Which operation is invalid for a Queue interface?Multiple choiceMedium
  7. What happens when you add a null element to a HashSet?Multiple choiceMedium
  8. When should you choose an ArrayList over an array in Java?Multiple choiceEasy
  9. Which collection would you choose to maintain insertion order of product reviews and why?Multiple choiceMedium
  10. Which method would you use to remove all elements from a HashSet?Multiple choiceMedium
  11. Which Java collection is best suited for iterating over elements in a predictable order using an enhanced for loop?Multiple choiceEasy
  12. In a queue implemented with ArrayDeque, what will the following code output? ArrayDeque<Integer> queue = new ArrayDeque<>(); queue.offer(1); queue.offer(2);…Multiple choiceMedium
  13. Given a HashMap<String, Integer> map, what will map.get(null) return if null is not a key in the map?Multiple choiceMedium
  14. Which Java collection should you choose for a product catalog if you need to frequently check if a product ID exists and order does not matter?Multiple choiceMedium
  15. How would you explain the time complexity of adding an element to an ArrayList when the underlying array needs resizing in an interview?Multiple choiceMedium
  16. Why is using an ArrayList over a LinkedList for storing product names faster in practice?Multiple choiceMedium
  17. Why can using an ArrayList over a LinkedList when frequently inserting elements at the beginning of the list become a problem in practice?Multiple choiceMedium
  18. Consider the following code snippet. What will be the output? ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(1, 3);…Multiple choiceHard
  19. If you need a sorted collection of unique product names with frequent reads, which collection is optimal?Multiple choiceHard
  20. What will HashMap's get() method return if you pass a key that doesn't exist in the map?Multiple choiceEasy
  21. How does an ArrayList handle element removal in terms of performance?Multiple choiceHard
  22. Why might using an ArrayList instead of an array be beneficial in a product catalog system?Multiple choiceHard
  23. You have a List of products and need to remove all items with a price below a certain threshold. Which iteration method is most efficient?Multiple choiceHard
  24. Which Java collection allows for iteration in reverse order using an iterator?Multiple choiceEasy
  25. Which of the following is a primary characteristic of a HashSet in Java?Multiple choiceEasy
  26. When would using a HashSet be more beneficial than using an ArrayList in a catalog?Multiple choiceMedium
  27. You have a collection of product prices that need frequent updates and occasional max price retrievals. Which collection strategy would be efficient?Multiple choiceHard
  28. Which operation is typically faster in a HashSet compared to a List?Multiple choiceMedium
  29. What happens when you try to add a duplicate element to a HashSet?Multiple choiceEasy
  30. Why do teams use a HashSet over a List when you need to store a collection of product names in production code?Multiple choiceEasy
  31. Which of the following statements about HashSet is true?Multiple choiceEasy
  32. Why might you choose to use a ListIterator over a basic Iterator for a List?Multiple choiceHard
  33. In an e-commerce catalog system, you want to store categories in a way that offers fast retrieval of a list of subcategories. What would be a suitable…Multiple choiceHard
  34. In which scenario would you choose a LinkedList over an ArrayList for your product catalog?Multiple choiceHard
  35. Which method should be used to add an element to the head of a Deque?Multiple choiceEasy
  36. What does the replace() method do in a HashMap when the key exists?Multiple choiceMedium
  37. Which Traversal method is safest for removing elements from a Collection while iterating?Multiple choiceMedium
  38. In a queue implemented with ArrayDeque, what is the time complexity of the offer() operation?Multiple choiceMedium
  39. In what scenario would you use containsKey() over get() when working with a HashMap?Multiple choiceEasy
  40. In which scenario would a LinkedList be more efficient than an ArrayList?Multiple choiceMedium
  41. Consider the following code snippet. What will be the output? HashMap<String, Integer> map = new HashMap<>(); map.put('A', 1); map.put('B', 2); map.put('A',…Multiple choiceHard
  42. When iterating over a HashMap using entrySet(), what type is each element?Multiple choiceMedium
  43. In what scenario might using a Map communicate better intent than a List?Multiple choiceMedium
  44. Which method would you use to remove an element from an ArrayList by its value?Multiple choiceMedium
  45. Why do teams use a Map over a List in Java in production code?Multiple choiceEasy
  46. Which method would you use to retrieve and remove the head of a Queue?Multiple choiceEasy
  47. The code repeatedly needs Product by SKU. Which model communicates that lookup need most clearly?Multiple choiceMedium
  48. In which scenario is using an iterator preferred over an enhanced for loop?Multiple choiceHard
  49. What does the size() method of a HashMap return?Multiple choiceEasy
  50. How would you explain the time complexity of checking if an element exists in a HashSet, assuming a good hash function in an interview?Multiple choiceMedium