Problem · Design

Key-Value Store GET and PUT QPS

Learn this problem
HardDatabricks logoDatabricksFULLTIMEPHONE SCREEN

Problem statement

Implement a key-value store that reports separate GET and PUT request rates over the trailing 300 seconds.

The function receives the server start time and a finite list of operations. Operations use nondecreasing integer-second timestamps and are processed in input order:

  • PUT|t|key|value stores or overwrites a string value and produces no output.
  • GET|t|key counts as a GET and produces VALUE:value when the key exists or NULL otherwise.
  • GET_LOAD|t counts as neither GET nor PUT and produces GET:a/b|PUT:c/d.

For a load query at time t, count requests in the half-open interval (t - 300, t]. Divide each count by min(t - serverStartTime, 300). Every GET_LOAD timestamp is strictly greater than the server start time. Write each fraction in lowest terms with a positive denominator; write zero as 0/1.

A request at the same timestamp as a GET_LOAD is included only when it appears earlier in the input.

Function

runLoadAwareStore(serverStartTime: long, operations: String[]) → String[]

Examples

Example 1

serverStartTime = 0operations = ["PUT|1|a|x","GET|2|a","GET|2|missing","GET_LOAD|3","GET_LOAD|301"]return = ["VALUE:x","NULL","GET:2/3|PUT:1/3","GET:1/150|PUT:0/1"]

The first load uses three seconds of uptime. At time 301, the PUT at time 1 is outside (1, 301], while both GET requests at time 2 remain in the window.

Constraints

  • 0 <= serverStartTime <= 10^9
  • 1 <= operations.length <= 200000
  • Operation timestamps are in [serverStartTime, 10^9] and are nondecreasing.
  • Every GET_LOAD time is greater than serverStartTime.
  • Keys and values contain 1 to 40 ASCII letters, digits, underscores, or hyphens.

More Databricks problems

drafts saved locally
public String[] runLoadAwareStore(long serverStartTime, String[] operations) {
  // write your code here
}
serverStartTime0
operations["PUT|1|a|x","GET|2|a","GET|2|missing","GET_LOAD|3","GET_LOAD|301"]
expected["VALUE:x", "NULL", "GET:2/3|PUT:1/3", "GET:1/150|PUT:0/1"]
checking account