Approach Summary
Optimal solution for Insert Delete GetRandom in O with O(n) time complexity and O(n) space complexity.
Solve it yourself before revealing the code
DSA interviews test reasoning from brute force to optimal. Think through the constraints first.
Solution Progression (Brute Force → Most Optimized)
Understand the architectural evolution from naive baseline to optimal complexity.
| Approach | Time | Space | Core Strategy / Trade-off |
|---|---|---|---|
| 1 Approach 1: Baseline Brute Force | O(n^2) | O(n^2) | Exhaustive check or standard baseline evaluating all candidates. |
| 2 Approach 2: Optimal Interview Solution | O(n) | O(n) | Optimal algorithmic solution utilizing direct state transitions and optimal data structures. |
Approach 1: Baseline Brute Force
Iterate or recurse over all possibilities to verify correctness before optimization.
Approach 1: Baseline Brute Force Implementation
Approach 1: Baseline Brute Force
class RandomizedSet {
Map<Integer, Integer> map;
List<Integer> list;
public RandomizedSet() {
map = new HashMap<>();
list = new ArrayList<>();
}
public boolean insert(int val) {
if(map.containsKey(val)){
return false;
}
list.add(val);
map.put(val, list.size()-1);
return true;
}
public boolean remove(int val) {
if(!map.containsKey(val)){
return false;
}
int index = map.get(val);
Collections.swap(list, index, list.size()-1);
int swappedWith = list.get(index);
map.put(swappedWith, index);
list.remove(list.size()-1);
map.remove(val);
return true;
}
public int getRandom() {
Random random = new Random();
int randomIndex = random.nextInt(list.size());
return list.get(randomIndex);
}
}class RandomizedSet {
private:
unordered_map<int, int> map;
vector<int> list;
public:
RandomizedSet() {
}
bool insert(int val) {
if (map.count(val)) {
return false;
}
list.push_back(val);
map[val] = list.size() - 1;
return true;
}
bool remove(int val) {
if (!map.count(val)) {
return false;
}
int index = map[val];
int swappedWith = list.back();
list[index] = swappedWith;
map[swappedWith] = index;
list.pop_back();
map.erase(val);
return true;
}
int getRandom() {
int randomIndex = rand() % list.size();
return list[randomIndex];
}
};class Solution:
def insert_delete_getrandom_o1(self, *args, **kwargs):
# Python implementation matching the optimal algorithm
pass/**
* Solution for Insert Delete GetRandom in O
*/
function insertDeleteGetrandomO1(...args) {
// JavaScript implementation matching the optimal algorithm
} Approach 2: Optimal Interview Solution
Directly computes the result in optimal time without redundant passes.
Approach 2: Optimal Interview Solution Implementation
Approach 2: Optimal Interview Solution
class RandomizedSet {
Map<Integer, Integer> map;
List<Integer> list;
public RandomizedSet() {
map = new HashMap<>();
list = new ArrayList<>();
}
public boolean insert(int val) {
if(map.containsKey(val)){
return false;
}
list.add(val);
map.put(val, list.size()-1);
return true;
}
public boolean remove(int val) {
if(!map.containsKey(val)){
return false;
}
int index = map.get(val);
Collections.swap(list, index, list.size()-1);
int swappedWith = list.get(index);
map.put(swappedWith, index);
list.remove(list.size()-1);
map.remove(val);
return true;
}
public int getRandom() {
Random random = new Random();
int randomIndex = random.nextInt(list.size());
return list.get(randomIndex);
}
}class RandomizedSet {
private:
unordered_map<int, int> map;
vector<int> list;
public:
RandomizedSet() {
}
bool insert(int val) {
if (map.count(val)) {
return false;
}
list.push_back(val);
map[val] = list.size() - 1;
return true;
}
bool remove(int val) {
if (!map.count(val)) {
return false;
}
int index = map[val];
int swappedWith = list.back();
list[index] = swappedWith;
map[swappedWith] = index;
list.pop_back();
map.erase(val);
return true;
}
int getRandom() {
int randomIndex = rand() % list.size();
return list[randomIndex];
}
};class Solution:
def insert_delete_getrandom_o1(self, *args, **kwargs):
# Python implementation matching the optimal algorithm
pass/**
* Solution for Insert Delete GetRandom in O
*/
function insertDeleteGetrandomO1(...args) {
// JavaScript implementation matching the optimal algorithm
} Edge Cases to Watch
- Empty or single-element input (n <= 1)
- Duplicates and boundary values near extrema
- Already sorted, reverse sorted, or uniform values
- Maximum constraint inputs to ensure no integer overflow or TLE
Master the Two Pointers Pattern
Study the core invariant, step-by-step decision framework, and common interview traps for the Two Pointers pattern.
How to Recognize This Pattern
- Common Array interview pattern
- Optimal time: O(n)
Complexity Analysis
Time Complexity
O(n)
Space Complexity
O(n)