Key-Value Store GET and PUT QPS
Learn this problemProblem 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|valuestores or overwrites a string value and produces no output.GET|t|keycounts as a GET and producesVALUE:valuewhen the key exists orNULLotherwise.GET_LOAD|tcounts as neither GET nor PUT and producesGET: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^91 <= 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.