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

  1. Choose the typical (average) time complexity of each operation.Matrix / gridMedium
  2. What does this print?What does this code output?Medium
  3. Which statement about nulls in maps is correct?Multiple choiceMedium
  4. Which interface should a class implement to define its natural ordering, so that Collections.sort(list) works without a comparator?Fill in the blankEasy
  5. If you need a collection that supports quick insertion and removal of elements at both ends, but also provides indexed access, which would you choose?Multiple choiceHard
  6. Why do teams use a HashSet over a List when storing items in production code?Multiple choiceMedium
  7. Which Java collection should you use if you need to store elements with fast random access and allow duplicates?Multiple choiceEasy
  8. Which collection does not allow duplicate elements?Multiple choiceEasy
  9. Which Java collection would you choose if you need to maintain insertion order and allow duplicates?Multiple choiceEasy
  10. How would you explain a key advantage of using a HashMap over a TreeMap in an interview?Multiple choiceMedium
  11. You are tasked with designing a cache system in Java where both insertion order and fast access are crucial. Which collection would you use?Multiple choiceHard
  12. You need a collection for an in-memory product catalog where order matters and items must be unique. Which collection is the best choice?Multiple choiceHard
  13. Which collection would you use for an in-memory store catalog if you need to frequently remove elements from both ends of the list?Multiple choiceHard
  14. Which collection would you use to store product reviews if order and fast access are both important, but insertion is infrequent?Multiple choiceHard
  15. Which scenario best justifies using a LinkedList over an ArrayList?Multiple choiceMedium
  16. Why might you choose a TreeMap over a HashMap for an in-memory store catalog?Multiple choiceMedium
  17. The hottest operation in the code is have we already processed this orderId?. Which collection usually follows from that workload?Multiple choiceMedium
  18. A shopping cart must preserve item order and allow the same product to appear more than once. Which collection contract fits best?Multiple choiceEasy
  19. If you need to frequently check the existence of items in a catalog by their names, which collection offers the best performance?Multiple choiceHard
  20. How would you explain the key difference between a Set and a List in a Java interview?Multiple choiceMedium
  21. In which scenario is it most beneficial to use a ConcurrentHashMap?Multiple choiceHard
  22. Which collection is best suited for implementing a priority queue?Multiple choiceEasy
  23. Which collection in Java should be used to store key-value pairs allowing null values but not null keys?Multiple choiceEasy
  24. How would you explain a strong modern Java choice for FIFO processing of order-review jobs in an interview?Multiple choiceMedium
  25. Given a List<String> catalog, which method call on an Iterator<String> allows safe removal of an item during iteration?Multiple choiceHard
  26. If you need to ensure that elements are processed in the exact order they are added but also need occasional access to both ends, which collection should you…Multiple choiceMedium
  27. Given an e-commerce application that requires frequent updates and lookups of product prices, which collection would you choose and why?Multiple choiceHard
  28. Which operation is most efficient for an ArrayList?Multiple choiceMedium
  29. What will happen if you try to access an index in an ArrayList that is out of bounds?Multiple choiceMedium
  30. You need to store product IDs and ensure no duplicates, with the ability to retrieve them in sorted order. Which collection do you choose?Multiple choiceMedium
  31. What will happen if you try to insert a null key into a HashMap?Multiple choiceMedium
  32. Which method checks if a HashSet is empty?Multiple choiceEasy
  33. If a custom object is used as an element in a HashSet, what must be correctly overridden?Multiple choiceHard
  34. When iterating over a HashMap, which method provides access to both keys and values?Multiple choiceMedium
  35. Which method would you use to check if a HashMap contains a specific key?Multiple choiceEasy
  36. Which method can you use to check if an ArrayList is empty?Multiple choiceEasy
  37. Why does the difference between a Queue and a Deque in Java matter in practice?Multiple choiceMedium
  38. What does calling list.set(1, 4) on an ArrayList containing [1, 2, 3] evaluate to?Multiple choiceMedium
  39. What does the 'contains' method of a HashSet use to check for the presence of an element?Multiple choiceMedium
  40. How does a HashSet handle collisions in hash values?Multiple choiceMedium
  41. In which scenario would a HashMap be more appropriate than a List?Multiple choiceMedium
  42. Which collection should you choose if you want to ensure the order of product IDs remains consistent with their insertion?Multiple choiceEasy
  43. Which collection automatically resizes itself to accommodate new elements?Multiple choiceEasy
  44. Which Java collection is typically used to store pairs of keys and values?Multiple choiceEasy
  45. How would you explain a key benefit of using an ArrayList when managing a dynamic list of products in an interview?Multiple choiceEasy
  46. When iterating over a HashMap, which method provides the most efficient way to traverse its entries?Multiple choiceMedium
  47. Which method would you use to remove a key-value pair from a HashMap?Multiple choiceEasy
  48. For which scenario is a Deque preferred over a Queue?Multiple choiceMedium
  49. Why might a poorly implemented hashCode() method impact the performance of a HashMap?Multiple choiceHard
  50. Why does the difference between an ArrayList and an array in Java matter in practice?Multiple choiceEasy