Hashmap
class. Let's design a new class - BetterMap
that supports the following 4 operations : .put(String key, Integer value)
.get(String key)
.remove(String key)
.pick()
pick()
should return a random Integer
from the values that are stored in your data structure (not the keys). Remember, this should happen in constant O(1) time!.put(K,V)
should replace V for calls with duplicate K keys.HashMap
s, etc in your implementation.bMap.put("a",1)
bMap.put("b",2)
bMap.put("c",3)
bMap.put("b",4)
bMap.get("b") => 4
bMap.delete("c")
bMap.pick() => randomly picks and returns 1 or 4 with equal probability
class BetterMap { public BetterMap() {} /** * Should insert the Key -> Value Mapping. Does not return anything * @param key * @param value */ public void put(String key, Integer value) {} /** * Should return the Value that was inserted for the corresponding Key. If * the Key does not exist, it will return null. * @param key * @return Integer value or null */ public Integer get(String key) {} /** * Should delete the mapping stored for the Key key. Does not return anything. * Should do nothing if the key does not exist. * @param key */ public void delete(String key) {} /** * Returns a Value picked at random from the existing Key -> Value data set. * This should run in constant O(1) time * @return Integer value from the dataset. Should never be null unless the dataset * is empty */ public Integer pick() {} }
C
Java
Python