Solution: Insert Delete GetRandom O(1)
Explore how to design a custom data structure that supports insert, delete, and getRandom operations all in average O(1) time. Understand the hybrid approach using arrays for indexing and hash maps for efficient element lookup and updates. This lesson helps you build a scalable solution suited for coding interviews that tests your knowledge of data structures and time complexity optimization.
Statement
Implement a Random Set data structure that can perform the following operations:
- Constructor(): This initializes the Random Set object.
- Insert(): This function takes an integer, data, as its parameter and, if it does not already exist in the set, add it to the set, returning TRUE. If the integer already exists in the set, the function returns FALSE.
- Delete(): This function takes an integer, data, as its parameter and, if it exists in the set, removes it, returning TRUE. If the integer does not exist in the set, the function returns FALSE.
- GetRandom(): This function takes no parameters. It returns an integer chosen at random from the set.
Note: Your implementation should aim to have a running time of (on average) for each operation.
Constraints:
- data
- No more than