Implement a Versioned Key-Value Store
Company: Netflix
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates understanding of time-versioned data storage, related data-structure selection, handling of overwrite semantics at equal timestamps, and the ability to reason about correctness and complexity trade-offs.
Constraints
- 0 <= n <= 20000, where n is the number of operations
- len(actions) == len(keys) == len(values) == len(timestamps)
- actions[i] is either "put" or "get"
- timestamps[i] are integers in the range [-10^9, 10^9]
- Keys and stored values are strings
Examples
Input: (['put', 'put', 'get', 'get', 'get'], ['campaignA', 'campaignA', 'campaignA', 'campaignA', 'campaignA'], ['v1', 'v2', '', '', ''], [10, 20, 15, 20, 5])
Expected Output: ['v1', 'v2', None]
Explanation: campaignA has versions at timestamps 10 and 20. Query 15 returns v1, query 20 returns v2, and query 5 has no earlier version.
Input: (['put', 'put', 'get', 'get'], ['k', 'k', 'k', 'k'], ['a', 'b', '', ''], [5, 5, 5, 6])
Expected Output: ['b', 'b']
Explanation: The second put overwrites the value for key k at the same timestamp 5. Both gets return the latest value b for timestamp 5.
Hints
- Store the version history for each key separately using a hash map.
- If each key keeps its timestamps sorted, you can use binary search to find the latest timestamp less than or equal to a query time.