Databricks questions

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) stores value under key, replacing any earlier value, and returns "OK".
  • get(at, key) returns the value stored under key, or "NULL" if key was never stored. The empty string is a real value, so a key stored with "" returns "", not "NULL". at is the integer time of the read; in this part it does not affect the result.
  1. 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) <= 10
  • 0 <= len(value) <= 10
  • 'a' <= key[i] <= 'z'
  • 1 <= len(operations) <= 10^4
  • at never decreases between calls to get