Implement Persistent KV Store Serialization
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of persistent storage design, binary-safe serialization, exact state restoration, and mutation logging for a key-value store, testing file I/O, data structures, and storage formats within the Coding & Algorithms domain.
Part 1: Serialize and Deserialize a Persistent In-Memory KV Store
Constraints
- `0 <= len(operations) <= 10^4`
- Keys and values are only of type `bytes` or `str`
- Total size of all live keys and values at any moment is at most `10^6` bytes
- Every `deserialize(path)` refers to a path that was previously serialized
Examples
Input: ([('put', 'a', '1'), ('get', 'a'), ('serialize', 'snap1'), ('put', 'a', '2'), ('deserialize', 'snap1'), ('get', 'a')],)
Expected Output: ['1', '1']
Explanation: After deserializing `snap1`, the old value `'1'` is restored.
Input: ([('put', b'a|b', b'v:1'), ('serialize', 'p'), ('delete', b'a|b'), ('get', b'a|b'), ('deserialize', 'p'), ('get', b'a|b')],)
Expected Output: [None, b'v:1']
Explanation: The format must not break when keys or values contain delimiter-like bytes.