Add a third dimension of time | CareerCup
Add a third dimension of time to a hashmap , so ur hashmap will look something like this - HashMap<K, t, V> where t is a float value. Implement the get and put methods to this map. The get method should be something like - map.get(K,t) which should give us the value. If t does not exists then map should return the closest t' such that t' is smaller than t. For example, if map contains (K,1,V1) and (K,2,V2) and the user does a get(k,1.5) then the output should be v1 as 1 is the next smallest number to 1.5
Read full article from Add a third dimension of time | CareerCup
Add a third dimension of time to a hashmap , so ur hashmap will look something like this - HashMap<K, t, V> where t is a float value. Implement the get and put methods to this map. The get method should be something like - map.get(K,t) which should give us the value. If t does not exists then map should return the closest t' such that t' is smaller than t. For example, if map contains (K,1,V1) and (K,2,V2) and the user does a get(k,1.5) then the output should be v1 as 1 is the next smallest number to 1.5
Key maps to a binary search tree in HashMap. First the key is mapped to a binary search tree and then the time is searched in the binary search tree to find either the time or smallest time less than the searched time. The skeleton of the design is presented below:
In java, we can use TreeMap.
BinarySearchTree<K, V>{
.......... // balanced binary search tree with K as key and V as the value
}
TimeStampHashMap<K, V>{
Map<K, BinarySearchTree<Float, V>> keyToBSTMap;
V get(K k, Float time){
return keyToBSTMap.get(k).binarySearch(time).getValue();
}
void put(K k , Float time, V value){
keyToBSTMap.get(k).insert(time, value);
}
}
Read full article from Add a third dimension of time | CareerCup