Key-Value Store with Hit Counter
DatabricksOnsiteMedium
Design a string-to-string store that also tracks how often each key is read. Every read is stamped with the time it happened; times never decrease across calls, and several reads may share one time. Each test drives a single KVStore instance through a sequence of method calls and checks every return value.
Part 1: Put and get
Implement two methods.
put(key, value)storesvalueunderkey, replacing any earlier value, and returns"OK".get(at, key)returns the value stored underkey, or"NULL"ifkeywas never stored. The empty string is a real value, so a key stored with""returns"", not"NULL".atis the integer time of the read; in this part it does not affect the result.
- Example 1
- init
[]- operations
[["get",[1,"a"]],["put",["a","x"]],["get",[2,"a"]],["put",["a","y"]],["get",[2,"a"]],["put",["b",""]],["get",[3,"b"]],["get",[3,"c"]]]- Output
["NULL","OK","x","OK","y","OK","","NULL"]
Why: A get on a missing key returns NULL. After put(a, x) the next get returns x, and overwriting a with y makes the following get return y. An empty string is a real value, so get(b) returns it rather than NULL.
Constraints
1 <= len(key) <= 100 <= len(value) <= 10'a' <= key[i] <= 'z'1 <= len(operations) <= 10^4atnever decreases between calls toget