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 4 of 4)
- 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
- Which method would you use to add an element to a HashMap?Multiple choiceEasy
- What happens if you call remove() on an empty Queue?Multiple choiceEasy
- Which method of the Iterator interface is used to check if there are more elements?Multiple choiceEasy
- Consider a HashSet with objects whose hashCode always returns the same value. What impact does this have?Multiple choiceHard
- What property of the HashSet collection makes it suitable for storing unique items in a catalog?Multiple choiceEasy
- Which collection would you use to implement a product catalog where each product can be accessed by its unique SKU quickly?Multiple choiceMedium
- How would you explain the return type of the next() method in an Iterator in an interview?Multiple choiceEasy