Implement a timestamped map
Company: Affirm
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to implement time-indexed key-value storage, covering data structure design, versioning semantics, and algorithmic complexity for efficient reads and writes.
Constraints
- 0 <= len(operations) <= 200000
- 1 <= timestamp <= 1000000000
- For the same key, put timestamps are strictly increasing
- Keys are non-empty strings
- Values are non-empty strings
Examples
Input: ([('put', 'foo', 'bar', 1), ('get', 'foo', 1), ('get', 'foo', 3), ('put', 'foo', 'bar2', 4), ('get', 'foo', 4), ('get', 'foo', 5)],)
Expected Output: ["bar", "bar", "bar2", "bar2"]
Explanation: At time 1 and 3, the latest value for 'foo' is 'bar'. After writing 'bar2' at time 4, queries at 4 and 5 return 'bar2'.
Input: ([('get', 'x', 10), ('put', 'x', 'a', 5), ('get', 'x', 4), ('get', 'x', 5), ('get', 'x', 6)],)
Expected Output: ["", "", "a", "a"]
Explanation: The first query has no prior write. After writing 'a' at time 5, a query at time 4 still has no answer, while queries at 5 and 6 return 'a'.
Input: ([('put', 'love', 'high', 10), ('put', 'love', 'low', 20), ('put', 'hate', 'meh', 15), ('get', 'love', 5), ('get', 'love', 10), ('get', 'love', 15), ('get', 'love', 20), ('get', 'hate', 15), ('get', 'hate', 100)],)
Expected Output: ["", "high", "high", "low", "meh", "meh"]
Explanation: Queries use the latest timestamp not greater than the requested time, independently for each key.
Input: ([],)
Expected Output: []
Explanation: With no operations, there are no get results.
Hints
- Store each key's history separately so you can search only within that key's values.
- Because timestamps for a key are already sorted, use binary search to find the rightmost timestamp less than or equal to the query time.