Implement a Rollback Key-Value Store
Company: Tesla
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates competency in designing data structures and state-management mechanisms, focusing on rollback/undo semantics, versioning, and efficient history tracking.
Constraints
- 0 <= len(operations) <= 200000
- Each key is a string of length 1 to 50
- Each value is an integer in the range [-10^9, 10^9]
- Average O(1) dictionary operations may be assumed
Examples
Input: [("set", "a", 1), ("get", "a"), ("rollback",), ("get", "a")]
Expected Output: [1, True, None]
Explanation: The key 'a' is created with value 1. get returns 1. rollback undoes that set, so 'a' is removed. The final get returns None.
Input: [("set", "a", 1), ("set", "a", 2), ("get", "a"), ("rollback",), ("get", "a"), ("rollback",), ("get", "a")]
Expected Output: [2, True, 1, True, None]
Explanation: The second set overwrites 'a' from 1 to 2. The first rollback restores 1. The second rollback removes 'a' entirely because the original set created it.
Input: [("set", "x", 5), ("delete", "x"), ("get", "x"), ("rollback",), ("get", "x")]
Expected Output: [None, True, 5]
Explanation: After deleting 'x', get returns None. rollback restores the deleted key with value 5.
Input: [("delete", "missing"), ("rollback",), ("set", "k", 7), ("delete", "missing"), ("rollback",), ("get", "k")]
Expected Output: [False, True, None]
Explanation: Deleting a missing key does nothing, so the first rollback has nothing to undo and returns False. After setting 'k', the later delete of 'missing' is still a no-op, so rollback undoes the set of 'k'.
Input: [("set", "a", -1), ("set", "b", 2), ("set", "a", 3), ("delete", "b"), ("get", "a"), ("get", "b"), ("rollback",), ("rollback",), ("get", "a"), ("get", "b")]
Expected Output: [3, None, True, True, -1, 2]
Explanation: Before rollback, 'a' is 3 and 'b' has been deleted. The first rollback restores 'b' = 2. The second rollback undoes the overwrite of 'a', restoring it to -1.
Input: []
Expected Output: []
Explanation: No operations produce no outputs.
Hints
- Instead of copying the entire store before each change, record only the information needed to undo the last mutation.
- A stack is a natural fit because rollback always undoes the most recent mutating operation.