Tags: , , , ,

# Design a data structure with insert delete and getRandom in O(1)

The problem statement is to design a data structure which performs the following operations in O(1) time complexity:
1. Insert an element, `insert(int value)`
2. Remove an element, `remove(int value)`
3. Get random element, `getRandom()`

For example, insert 1 into the data structure insert(1): [1]
insert 2 into the data structure insert(2): [1,2]
insert 3 into the data structure insert(3): [1,2,3]

Remove 2 from it, remove(2). [1,3]
getRandom() should return 1 and 3 with equal probabilities.

These kind of problems are easy and hard at the same time. Idea is to go step by step and solve each part. The first step is to define an interface for this data structure, which is easy given the definition of the problem.

```public interface IRandomNumberGenerator {
public boolean insert(int value);
public boolean remove (int value);
public int getRandom();
}
```

Now that interface is ready, time to start implementing the class which implements this interface. First of all, we have to find a container to store all the elements. If we take an ArrayList, `insert()` is O(1) as we will always add new element at the end of the ArrayList. `getRandom` is also O(1). However, there is problem with `remove()`. To remove an element from ArrayList, we have to scan the whole ArrayList and remove the element, the move all the elements on the right of the deleted element to one index left. This is O(n) operation.

## Insert delete and getRandom in O(1): selection of data structures

A problem with storing elements in an ArrayList is that while removal, we have to scan the list and find the location of the element to be removed. What if we already knew the location of the element? If we store the position of each element in ArrayList in a HashMap which maps the value to its index on ArrayList

Now, `insert()` has to insert a value to two data structures, first into the ArrayList and then the location of the value in ArrayList to the HashMap. Remove operation can simply go to the location in the ArrayList and delete the element. Wait, still, we have to move all the elements on the right one position left. It means the worst case complexity of `remove()` still O(n).

We know one thing: if I remove the last element from the ArrayList then there is no shifting required. What if we copy the last value at the index of the element to be removed and then just remove the last element. Be careful, we have to update the HashMap with the new value for the element at the last index of ArrayList. In this way, `remove()` is also O(1).

### Insert, delete and getRandom in O(1): implementation

```package AlgorithmsAndMe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class RandomNumberGenerator implements IRandomNumberGenerator {

private ArrayList<Integer> list;
private Map<Integer, Integer> loc;
private Random random;

//Initializing the class
public RandomNumberGenerator(){
list = new ArrayList<>();
loc = new HashMap<>();
random = new Random();
}

@Override
public boolean insert(int value) {
/*If hash already contains key then it is a duplicate key.
So, we just return false.
*/
if(loc.containsKey(value)) return false;

//Insert into list

//Save the location on hash map
loc.put(value, list.size()-1);
return true;
}

@Override
public boolean remove(int value) {
/* If there is no entry in hash, that means
there is no element in ArrayList */
if(!loc.containsKey(val)) return false;

int location = loc.get(val);
//Remove from hash
loc.remove(val);

if(location != list.size()-1){
/*Copy the last value in the array
list to the current location*/
list.set(location, list.get(list.size()-1));

//Update the location of last element in hash
loc.put(list.get(location), location);
}

//remove the last location from ArrayList
list.remove(list.size()-1);

return true;
}

@Override
public int getRandom() {
return list.get(random.nextInt(list.size()));
}
}

```
```package AlgorithmsAndMe;

import static org.junit.Assert.*;

public class RandomNumberGeneratorTest {

RandomNumberGenerator randomNumberGenerator =
new RandomNumberGenerator();

@org.junit.Test
public void testInterface() {
assertEquals(true, randomNumberGenerator.insert(4));
assertEquals(true, randomNumberGenerator.insert(5));
assertEquals(true, randomNumberGenerator.insert(3));
assertEquals(true, randomNumberGenerator.insert(2));

assertEquals(true, randomNumberGenerator.remove(4));

int random = randomNumberGenerator.getRandom();
System.out.println(random);
}
}
```

The complexity of the whole data structure for insert, delete and getRandom is O(1).

#### Insert, delete and get random when duplicates are allowed

Let’s make this problem a bit more complex by making duplicate elements possible in the list. The first problem with the existing implementation is that it stores the location of an element in ArrayList in a HashMap. If the same element can appear multiple times in the list, then which location should we store? We should store all the locations. It will change the definition of our HashMap as

```Map<Integer, HashSet<Integer>>
```

Hashset implements the Set interface, backed by a hash table which is actually a HashMap instance. No guarantee is made as to the iteration order of the set which means that the class does not guarantee the constant order of elements over time, that is what we require. We require that insert and remove operation on this data structure should be O(1) or constant time complexity.
To know more about the complexity of various data structures in Java, follow Runtime Complexity of Java Collections and read reason why HashSet provides constant time insert and remove operations.
Everything else follows the same process. To `insert()`, we should insert the location of the element at the HashSet in the hash table. While removing we find the last location of the element, put the last element of ArrayList in that location and update the HashSet of the location corresponding to the value at the last index of the ArrayList. Remove the last element from ArrayList.
We also have to move the last element in ArrayList of location in Hash, which is O(1) operation.

`getRandom()` implementation remains same.

```package AlgorithmsAndMe;

import java.util.*;

public class RandomNumberGenerator implements IRandomNumberGenerator {

private ArrayList<Integer> list;
private Map<Integer, HashSet<Integer>> loc;
private Random random;

//Initializing the class
public RandomNumberGenerator(){
list = new ArrayList<>();
loc = new HashMap<>();
random = new Random();
}

@Override
public boolean insert(int value) {

if(!loc.containsKey(value)){
loc.put(value, new HashSet<>());
};

//Insert into list

//Save the location on hash map
return true;
}

@Override
public boolean remove(int value) {
/* If there is no entry in hash, that means
there is no element in ArrayList */
if(!loc.containsKey(value)) return false;

//Get the last location of the element in ArrayList
HashSet<Integer> listLocations = loc.get(value);
int location = listLocations.iterator().next();
loc.get(value).remove(location);

int lastElement = list.get(list.size()-1);
if( lastElement != value) {
/*Copy the last value in the array
list to the current location*/
list.set(location, lastElement);
//Update the location of last element in hash
loc.get(lastElement).remove(list.size()-1);
}
//remove the last location from ArrayList
list.remove(list.size()-1);

if(listLocations.isEmpty()) loc.remove(value);
return true;
}

@Override
public int getRandom() {
return list.get(random.nextInt(list.size()));
}
}

```

Other problems which are very similar to this concept are: design an LRU cache, first non-repeated character in stream etc.

Please share if there is anything wrong or missing. If you are preparing for an interview and need one to one personalized coaching, please reach out to us on communications@algorithmsandme.com