Problem · Design

Time-Based Key-Value Database

Learn this problem
MediumIllumio logoIllumioFULLTIMEONSITE INTERVIEW

Problem statement

Process operations on a time-based key-value database.

Each operation is one of:

  • ["SET", key, value, timestamp]: store value for key at the integer timestamp.
  • ["GET", key, timestamp]: return the value stored for key at the greatest timestamp less than or equal to the requested timestamp. Return the empty string when no such value exists.

SET timestamps are strictly increasing for each individual key. GET timestamps may arrive in any order. Return one string for every GET operation, in operation order.

Function

runTimeDatabase(operations: String[][]) → String[]

Examples

Example 1

operations = [["SET","foo","bar","1"],["GET","foo","1"],["GET","foo","3"],["SET","foo","bar2","4"],["GET","foo","4"],["GET","foo","5"]]return = ["bar","bar","bar2","bar2"]

The first two reads use the value stored at timestamp 1. Once bar2 is stored at timestamp 4, reads at 4 and later return it.

Example 2

operations = [["GET","missing","2"],["SET","a","one","3"],["SET","b","two","5"],["GET","a","2"],["GET","a","10"],["GET","b","5"]]return = ["","","one","two"]

A missing key and a query before a key's first timestamp both return the empty string. Keys keep independent histories.

Constraints

  • 1 <= operations.length <= 20000
  • Every operation has a valid name and arity.
  • Keys and stored values are non-empty strings.
  • 0 <= timestamp <= 10^9
  • SET timestamps are strictly increasing for each key.
drafts saved locally
public String[] runTimeDatabase(String[][] operations) {
    // Write your code here.
}
operations[["SET","foo","bar","1"],["GET","foo","1"],["GET","foo","3"],["SET","foo","bar2","4"],["GET","foo","4"],["GET","foo","5"]]
expected["bar", "bar", "bar2", "bar2"]
checking account