Time-Based Key-Value Store
Try to solve the Time Based Key-Value Store problem.
Statement
Design a data structure that can store multiple values of the same key at different timestamps and retrieve the key’s value at a certain timestamp.
Implement a struct, TimeStamp, which should provide the following functionalities:
-
NewTimeStamp(): This function initializes the values dictionary and timestamp dictionary.
-
Set Value(key, value, timestamp): This function stores the key and value at any given timestamp.
-
Get Value(key, timestamp): This function returns the value set for this key at the specified timestamp.
Note: When a query requests the value of a key at a timestamp that isn’t recorded, return the value corresponding to the most recent timestamp before the query’s timestamp. If there are no timestamps before the query’s timestamp, return an empty string.
Constraints:
-
key.length
,value.length
key
andvalue
consist of lowercase English letters and digits.-
timestamp
- At most calls will be made to Set Value and Get Value.
- All the timestamps,
timestamp
, of Set Value are strictly increasing.
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps us to check that you’re solving the correct problem:
Time-Based Key-Value Store
Given this series of commands, what is the output of the last command?
NewTimeStamp()
Set Value(“prediction”, “winning”, 3)
Get Value(“prediction”, 2)
“winning”
“losing”
“”
Figure it out!
We have a game for you to play. Rearrange the logical building blocks required to implement Get Value().
Try it yourself
Implement your solution in the following coding playground.
package main// TimeStamp data structuretype TimeStamp struct {// write your code here}// TimeStamp constructorfunc NewTimeStamp() TimeStamp {// Replace this placeholder return statement with your codereturn TimeStamp{}}// Set TimeStamp data variablesfunc (this *TimeStamp) SetValue(key string, value string, timeStamp int) bool {// Replace this placeholder return statement with your codereturn false}// Get the value for the given key and timestampfunc (this *TimeStamp) GetValue(key string, timestamp int) string {// Replace this placeholder return statement with your codereturn ""}