Design a Set with $O(1)$ `insert`, `remove`, and `getRandom`

Data Structures
Medium
Google
97.1K views

Design a data structure that supports inserting a value, removing a value, and getting a random element, all in $O(1)$ average time. This requires combining an Array and a Hash Map.

Why Interviewers Ask This

Google interviewers ask this to evaluate your ability to balance time and space complexity trade-offs. They specifically test if you understand that Arrays provide O(1) random access while Hash Maps offer O(1) lookups, but neither alone supports efficient removal from the middle without shifting elements or scanning. This question probes your depth in combining data structures to solve constraints that a single structure cannot handle.

How to Answer This Question

1. Clarify requirements: Confirm that 'average' O(1) is acceptable and that duplicates are not allowed unless specified. 2. Propose the hybrid approach: Explain that you will use a dynamic array (or list) for storage to enable constant-time random access via index, paired with a hash map mapping values to their indices. 3. Detail Insertion: Describe adding the value to the end of the array and storing its index in the map. 4. Explain Removal Strategy: Highlight the critical trick of swapping the element to be removed with the last element in the array before popping it, then updating the map entry for the swapped element. 5. Discuss Random Access: Reiterate that picking a random index from the array yields an O(1) result. 6. Analyze Complexity: Explicitly state that all three operations remain O(1) on average due to these mechanisms.

Key Points to Cover

  • Explicitly stating the need to swap the target element with the last element during removal
  • Explaining how the hash map tracks indices to allow O(1) lookup after the swap
  • Clarifying that duplicates are typically excluded in standard set implementations
  • Acknowledging the distinction between worst-case O(1) and average O(1) due to hash collisions
  • Demonstrating awareness of space complexity being linear relative to the number of elements

Sample Answer

To achieve O(1) for insert, remove, and getRandom, I would combine a dynamic array with a hash map. The array allows us to access any element by index in constant time, which solves the getRandom requirement. However, removing an arbitrary element from an array usually takes O(n) because we must shift subsequent elements. To avoid this, we use the hash map to store the index of every value currently in the set. For insertion, we simply append the new value to the end of the array and record its index in the map. For removal, instead of shifting elements, we swap the target value with the last element in the array. We then pop the last element and update the map entry for the swapped value to reflect its new index. Finally, getRandom just generates a random integer between zero and the array size minus one and returns the element at that index. This approach ensures that no matter how many elements we have, all operations rely only on direct indexing or hash lookups, maintaining O(1) performance. This technique demonstrates the kind of optimization Google values: solving complex constraints through clever structural combinations rather than brute force.

Common Mistakes to Avoid

  • Attempting to remove an element by shifting all subsequent items in the array, resulting in O(n) removal time
  • Using only a hash map without an array, which makes retrieving a random element inefficient as it requires iterating keys
  • Failing to update the hash map when swapping elements, leading to incorrect indices and runtime errors
  • Overlooking edge cases like removing the last element or inserting into an empty set without proper checks

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 87 Google questions