Problem · Hash Table

Time-Based Key-Value Store

Learn this problem
MediumOracle logoOracleFULLTIMEPHONE SCREEN

Problem statement

Process timestamped set and get operations on a time-based key-value store.

  • A set stores the supplied value for its key and timestamp and returns "null".
  • A get returns the value for its key with the greatest stored timestamp that is at most the query timestamp. If no such value exists, return the empty string.

Process operations in their array order and return one result per operation.

Function

runTimeMap(operations: String[], keys: String[], values: String[], timestamps: int[]) → String[]

Examples

Example 1

operations = ["set","get","get","set","get"]keys = ["foo","foo","foo","foo","foo"]values = ["bar","","","bar2",""]timestamps = [1,1,3,4,4]return = ["null","bar","bar","null","bar2"]

Each query returns the latest value for foo whose stored timestamp does not exceed the query timestamp.

Example 2

operations = ["get","set","get"]keys = ["x","x","x"]values = ["","one",""]timestamps = [2,5,4]return = ["","null",""]

Neither query has a stored version at or before its timestamp.

Constraints

  • 1 <= operations.length <= 100000, and all four input arrays have the same length.
  • Each operation is exactly "set" or "get".
  • 1 <= key.length, value.length <= 100.
  • 1 <= timestamp <= 10^9.
  • For each key, timestamps supplied to set are strictly increasing.

More Oracle problems

drafts saved locally
public String[] runTimeMap(String[] operations, String[] keys, String[] values, int[] timestamps) {
    // Write your code here.
}
operations["set","get","get","set","get"]
keys["foo","foo","foo","foo","foo"]
values["bar","","","bar2",""]
timestamps[1,1,3,4,4]
expected["null", "bar", "bar", "null", "bar2"]
checking account