Problem · Hash Table

Simplified Time-Based Key-Value Store

Learn this problem
MediumSpaceX logoSpaceXFULLTIMEPHONE SCREEN

Problem statement

Implement a simplified time-based key-value store by processing a sequence of operations in order.

Each operation is represented as an array of strings:

  • ["set", key, value, timestamp] stores value for key at timestamp.
  • ["get", key, timestamp] asks for the value stored for key at the greatest set timestamp less than or equal to timestamp.

The timestamp in each operation is a base-10 integer encoded as a string. If a get operation has no qualifying stored value, its result is the empty string. Return the results of the get operations in their original order; set operations do not add an entry to the returned array.

Function

timeMapResults(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 queries use the value stored at timestamp 1. After the second set, queries at timestamps 4 and 5 use bar2.

Example 2

operations = [["get","missing","7"],["set","x","one","10"],["get","x","9"],["get","x","10"]]return = ["","","one"]

The missing key and the query before x is first stored both return an empty string. The exact-timestamp query returns one.

Example 3

operations = [["set","alpha","a1","2"],["set","beta","b1","3"],["set","alpha","a2","8"],["get","alpha","7"],["get","beta","100"],["get","alpha","8"]]return = ["a1","b1","a2"]

Histories are independent by key, and each query uses the latest qualifying timestamp for that key.

Constraints

  • 1 <= operations.length <= 200000
  • Every operation begins with "set" or "get".
  • A set operation has exactly four fields; a get operation has exactly three fields.
  • 1 <= key.length, value.length <= 100, and keys and values contain only lowercase English letters and digits.
  • Every timestamp is a base-10 integer in the range [1, 10000000].
  • The timestamps of set operations are strictly increasing.

More SpaceX problems

drafts saved locally
public String[] timeMapResults(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