Problem
Design a key-value store that keeps many values for each key. A key-value store keeps values and finds each value by its key. Each value also has a timestamp. A timestamp is an integer that shows a point in time.
Write the class TimeMap with these operations:
TimeMap() creates an empty store. set(key, value, timestamp) stores value for key with the timestamp timestamp. get(key, timestamp) returns one value for key.
The value from get comes from the set call with the largest timestamp that is not greater than timestamp. If no such call exists, get returns an empty string "".
The timestamps of the set calls always increase.
Example 1
- Input:
- ["TimeMap","set","get","get","set","get","get"] [[],["color","red",2],["color",1],["color",3],["color","blue",6],["color",5],["color",9]]
- Output:
- [null,null,"","red",null,"red","blue"]
- Explanation:
get("color", 1)returns an empty string, because the first value has the timestamp 2.get("color", 5)returns"red", because"blue"has the later timestamp 6.
Example 2
- Input:
- ["TimeMap","set","set","get","get"] [[],["a","x",1],["b","y",2],["a",2],["b",1]]
- Output:
- [null,null,null,"x",""]
- Explanation:
- Each key has its own values. The key
bhas no value at the timestamp 1.
Constraints
- 1 <= key.length, value.length <= 100
- key and value contain only lowercase English letters and digits.
- 1 <= timestamp <= 10^7
- The timestamps of the set calls are strictly increasing.
- At most 2 * 10^5 calls to set and get
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Design Excel Sum Formula
Coding · Array · Design · Graph Theory
- Design In-Memory File System
Coding · Design · Hash Table · Sorting
- Design Spreadsheet
Coding · Array · Design · Hash Table
- Encrypt and Decrypt Strings
Coding · Array · Design · Hash Table
Sources · 3 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 9 records
Amazon
Leetcode 981. Time Based Key-Value Store
Reported Feb 3, 2025 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026
View sourceApple
Leetcode 981. Time Based Key-Value Store
Reported Jul 31, 2026 · 4 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceCoupang
Leetcode 981. Time Based Key-Value Store
Reported Aug 1, 2026 · 1 source report
STAFF
Source snapshot: Nov 12, 2026
View sourceGusto
Leetcode 981. Time Based Key-Value Store
Reported Mar 20, 2026 · 3 source reports
STAFF
Source snapshot: Nov 12, 2026