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 2 of 4)
- In a HashMap, what happens if you put() a new value for an existing key?Multiple choiceMedium
- For storing a list of unique user IDs where insertion order must be preserved, which collection is most appropriate?Multiple choiceMedium
- What will be the output when attempting to add a duplicate element to a HashSet?Multiple choiceMedium
- Which of the following statements about the Iterator interface is true?Multiple choiceMedium
- How does the ArrayDeque handle the addition of elements when the underlying array is full?Multiple choiceHard
- Which operation is invalid for a Queue interface?Multiple choiceMedium
- What happens when you add a null element to a HashSet?Multiple choiceMedium
- When should you choose an ArrayList over an array in Java?Multiple choiceEasy
- Which collection would you choose to maintain insertion order of product reviews and why?Multiple choiceMedium
- Which method would you use to remove all elements from a HashSet?Multiple choiceMedium
- Which Java collection is best suited for iterating over elements in a predictable order using an enhanced for loop?Multiple choiceEasy
- 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
- Given a HashMap<String, Integer> map, what will map.get(null) return if null is not a key in the map?Multiple choiceMedium
- 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
- 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
- Why is using an ArrayList over a LinkedList for storing product names faster in practice?Multiple choiceMedium
- 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
- 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
- If you need a sorted collection of unique product names with frequent reads, which collection is optimal?Multiple choiceHard
- What will HashMap's get() method return if you pass a key that doesn't exist in the map?Multiple choiceEasy
- How does an ArrayList handle element removal in terms of performance?Multiple choiceHard
- Why might using an ArrayList instead of an array be beneficial in a product catalog system?Multiple choiceHard
- 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
- Which Java collection allows for iteration in reverse order using an iterator?Multiple choiceEasy
- Which of the following is a primary characteristic of a HashSet in Java?Multiple choiceEasy
- When would using a HashSet be more beneficial than using an ArrayList in a catalog?Multiple choiceMedium
- You have a collection of product prices that need frequent updates and occasional max price retrievals. Which collection strategy would be efficient?Multiple choiceHard
- Which operation is typically faster in a HashSet compared to a List?Multiple choiceMedium
- What happens when you try to add a duplicate element to a HashSet?Multiple choiceEasy
- Why do teams use a HashSet over a List when you need to store a collection of product names in production code?Multiple choiceEasy
- Which of the following statements about HashSet is true?Multiple choiceEasy
- Why might you choose to use a ListIterator over a basic Iterator for a List?Multiple choiceHard
- 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
- In which scenario would you choose a LinkedList over an ArrayList for your product catalog?Multiple choiceHard
- Which method should be used to add an element to the head of a Deque?Multiple choiceEasy
- What does the replace() method do in a HashMap when the key exists?Multiple choiceMedium
- Which Traversal method is safest for removing elements from a Collection while iterating?Multiple choiceMedium
- In a queue implemented with ArrayDeque, what is the time complexity of the offer() operation?Multiple choiceMedium
- In what scenario would you use containsKey() over get() when working with a HashMap?Multiple choiceEasy
- In which scenario would a LinkedList be more efficient than an ArrayList?Multiple choiceMedium
- 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
- When iterating over a HashMap using entrySet(), what type is each element?Multiple choiceMedium
- In what scenario might using a Map communicate better intent than a List?Multiple choiceMedium
- Which method would you use to remove an element from an ArrayList by its value?Multiple choiceMedium
- Why do teams use a Map over a List in Java in production code?Multiple choiceEasy
- Which method would you use to retrieve and remove the head of a Queue?Multiple choiceEasy
- The code repeatedly needs Product by SKU. Which model communicates that lookup need most clearly?Multiple choiceMedium
- In which scenario is using an iterator preferred over an enhanced for loop?Multiple choiceHard
- What does the size() method of a HashMap return?Multiple choiceEasy
- 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