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

  1. You have a list of product descriptions that are mostly accessed in sequential order but occasionally need to be searched. Which collection is generally more…Multiple choiceMedium
  2. Which method would you use to add an element to a HashMap?Multiple choiceEasy
  3. What happens if you call remove() on an empty Queue?Multiple choiceEasy
  4. Which method of the Iterator interface is used to check if there are more elements?Multiple choiceEasy
  5. Consider a HashSet with objects whose hashCode always returns the same value. What impact does this have?Multiple choiceHard
  6. What property of the HashSet collection makes it suitable for storing unique items in a catalog?Multiple choiceEasy
  7. Which collection would you use to implement a product catalog where each product can be accessed by its unique SKU quickly?Multiple choiceMedium
  8. How would you explain the return type of the next() method in an Iterator in an interview?Multiple choiceEasy