Build a time-based key-value store
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of time-versioned data structures and efficient temporal lookup within associative mappings, focusing on storing and retrieving multiple values per key across timestamps.
Constraints
- 0 <= len(ops) == len(keys) == len(values) == len(timestamps) <= 200000
- ops[i] is either "set" or "get"
- 1 <= timestamps[i] <= 10^9
- key and value are strings
- For any fixed key, timestamps for its set operations are strictly increasing
Examples
Input: (['set', 'get', 'get', 'set', 'get', 'get'], ['foo', 'foo', 'foo', 'foo', 'foo', 'foo'], ['bar', '', '', 'bar2', '', ''], [1, 1, 3, 4, 4, 5])
Expected Output: ['bar', 'bar', 'bar2', 'bar2']
Explanation: The key 'foo' has values 'bar' at time 1 and 'bar2' at time 4. Each get returns the most recent value at or before the requested timestamp.
Input: (['get', 'set', 'get', 'get'], ['a', 'a', 'a', 'b'], ['', 'x', '', ''], [1, 5, 4, 2])
Expected Output: ['', '', '']
Explanation: The first get asks for a key that does not exist yet. After setting 'a' at time 5, querying at time 4 still returns an empty string because no timestamp <= 4 exists. Key 'b' was never set.
Hints
- Store all timestamps and values for each key together so you can search only within that key's history.
- Because timestamps for each key are already sorted, use binary search to find the rightmost timestamp less than or equal to the query time.