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 1 of 4)
- Choose the typical (average) time complexity of each operation.Matrix / gridMedium
- What does this print?What does this code output?Medium
- Which statement about nulls in maps is correct?Multiple choiceMedium
- Which interface should a class implement to define its natural ordering, so that Collections.sort(list) works without a comparator?Fill in the blankEasy
- 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
- Why do teams use a HashSet over a List when storing items in production code?Multiple choiceMedium
- Which Java collection should you use if you need to store elements with fast random access and allow duplicates?Multiple choiceEasy
- Which collection does not allow duplicate elements?Multiple choiceEasy
- Which Java collection would you choose if you need to maintain insertion order and allow duplicates?Multiple choiceEasy
- How would you explain a key advantage of using a HashMap over a TreeMap in an interview?Multiple choiceMedium
- 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
- 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
- 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
- Which collection would you use to store product reviews if order and fast access are both important, but insertion is infrequent?Multiple choiceHard
- Which scenario best justifies using a LinkedList over an ArrayList?Multiple choiceMedium
- Why might you choose a TreeMap over a HashMap for an in-memory store catalog?Multiple choiceMedium
- The hottest operation in the code is have we already processed this orderId?. Which collection usually follows from that workload?Multiple choiceMedium
- A shopping cart must preserve item order and allow the same product to appear more than once. Which collection contract fits best?Multiple choiceEasy
- If you need to frequently check the existence of items in a catalog by their names, which collection offers the best performance?Multiple choiceHard
- How would you explain the key difference between a Set and a List in a Java interview?Multiple choiceMedium
- In which scenario is it most beneficial to use a ConcurrentHashMap?Multiple choiceHard
- Which collection is best suited for implementing a priority queue?Multiple choiceEasy
- Which collection in Java should be used to store key-value pairs allowing null values but not null keys?Multiple choiceEasy
- How would you explain a strong modern Java choice for FIFO processing of order-review jobs in an interview?Multiple choiceMedium
- Given a List<String> catalog, which method call on an Iterator<String> allows safe removal of an item during iteration?Multiple choiceHard
- 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
- Given an e-commerce application that requires frequent updates and lookups of product prices, which collection would you choose and why?Multiple choiceHard
- Which operation is most efficient for an ArrayList?Multiple choiceMedium
- What will happen if you try to access an index in an ArrayList that is out of bounds?Multiple choiceMedium
- 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
- What will happen if you try to insert a null key into a HashMap?Multiple choiceMedium
- Which method checks if a HashSet is empty?Multiple choiceEasy
- If a custom object is used as an element in a HashSet, what must be correctly overridden?Multiple choiceHard
- When iterating over a HashMap, which method provides access to both keys and values?Multiple choiceMedium
- Which method would you use to check if a HashMap contains a specific key?Multiple choiceEasy
- Which method can you use to check if an ArrayList is empty?Multiple choiceEasy
- Why does the difference between a Queue and a Deque in Java matter in practice?Multiple choiceMedium
- What does calling list.set(1, 4) on an ArrayList containing [1, 2, 3] evaluate to?Multiple choiceMedium
- What does the 'contains' method of a HashSet use to check for the presence of an element?Multiple choiceMedium
- How does a HashSet handle collisions in hash values?Multiple choiceMedium
- In which scenario would a HashMap be more appropriate than a List?Multiple choiceMedium
- Which collection should you choose if you want to ensure the order of product IDs remains consistent with their insertion?Multiple choiceEasy
- Which collection automatically resizes itself to accommodate new elements?Multiple choiceEasy
- Which Java collection is typically used to store pairs of keys and values?Multiple choiceEasy
- How would you explain a key benefit of using an ArrayList when managing a dynamic list of products in an interview?Multiple choiceEasy
- When iterating over a HashMap, which method provides the most efficient way to traverse its entries?Multiple choiceMedium
- Which method would you use to remove a key-value pair from a HashMap?Multiple choiceEasy
- For which scenario is a Deque preferred over a Queue?Multiple choiceMedium
- Why might a poorly implemented hashCode() method impact the performance of a HashMap?Multiple choiceHard
- Why does the difference between an ArrayList and an array in Java matter in practice?Multiple choiceEasy