Skip to content
BASA DSA

Time Based Key Value Store

MediumBinary Search

Implement TimeMap. set(key,value,timestamp) saves a version; get(key,timestamp) returns the value with greatest saved time no later than timestamp, or an empty string if none exists.

Example 1

Input
arg 1 = ["TimeMap","set","set","get","get","get"]
arg 2 = [[],["a","old",2],["a","new",5],["a",1],["a",4],["a",9]]
Output
[null,null,null,"","old","new"]

Explanation

Time 1 precedes all versions; time 4 selects old at 2, and time 9 selects new at 5.

Example 2

Input
arg 1 = ["TimeMap","get","set","get"]
arg 2 = [[],["z",10],["z","ok",10],["z",10]]
Output
[null,"",null,"ok"]

Explanation

A missing key returns an empty string; an exact-time lookup after set returns ok.

Example 3

Input
arg 1 = ["TimeMap","set","set","get","get"]
arg 2 = [[],["a","one",1],["b","two",2],["a",3],["b",1]]
Output
[null,null,null,"one",""]

Explanation

Updating key b does not change key a; querying a at time 3 selects its version from time 1.

Constraints

  • At most 200000 operations
  • set timestamps are strictly increasing globally and positive
  • Keys and values are nonempty ASCII strings

Class contractTimeMapArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…