Implement a serializable key-value store
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates competency in implementing robust serialization and in-memory data structures, covering binary format design, handling arbitrary bytes and Unicode, correctness across edge cases (empty state, large keys/values, overwrites, deletions), input validation and failure recovery, complexity analysis, and unit testing.
Constraints
- 1 <= len(operations) <= 200000
- The total number of bytes appearing in all keys, values, and serialized blobs is at most 10^6
- Each key and value length is between 0 and 2^32 - 1 bytes
- Average-case O(1) map operations are acceptable; each serialize/deserialize must be linear in the blob size
Examples
Input: [('put', b'name', b'alice'), ('put', b'lang', b'py'), ('get', b'name'), ('serialize',), ('erase', b'name'), ('get', b'name'), ('deserialize', b'KVS1\x00\x00\x00\x02\x00\x00\x00\x04name\x00\x00\x00\x05alice\x00\x00\x00\x04lang\x00\x00\x00\x02py'), ('get', b'name'), ('get', b'lang')]
Expected Output: [b'alice', b'KVS1\x00\x00\x00\x02\x00\x00\x00\x04name\x00\x00\x00\x05alice\x00\x00\x00\x04lang\x00\x00\x00\x02py', None, True, b'alice', b'py']
Explanation: The store is serialized with two entries, then one key is deleted. Deserializing the earlier blob restores both pairs.
Input: [('serialize',), ('deserialize', b'KVS1\x00\x00\x00\x00'), ('get', b'missing')]
Expected Output: [b'KVS1\x00\x00\x00\x00', True, None]
Explanation: An empty store serializes to just the header and a zero entry count. Deserializing it succeeds and the store remains empty.
Input: [('put', b'a', b'1'), ('put', b'a', b'2'), ('serialize',), ('erase', b'a'), ('get', b'a'), ('deserialize', b'KVS1\x00\x00\x00\x01\x00\x00\x00\x01a\x00\x00\x00\x012'), ('get', b'a')]
Expected Output: [b'KVS1\x00\x00\x00\x01\x00\x00\x00\x01a\x00\x00\x00\x012', None, True, b'2']
Explanation: Overwriting the same key updates the value instead of creating a second copy. The serialized blob contains only one entry for key b'a'.
Input: [('put', b'\xcf\x80', b'\x00\xffA'), ('get', b'\xcf\x80'), ('serialize',)]
Expected Output: [b'\x00\xffA', b'KVS1\x00\x00\x00\x01\x00\x00\x00\x02\xcf\x80\x00\x00\x00\x03\x00\xffA']
Explanation: The format supports arbitrary bytes in both keys and values, including non-ASCII bytes and embedded zero bytes.
Input: [('put', b'x', b'1'), ('deserialize', b'KVS1\x00\x00\x00\x01\x00\x00\x00\x01x'), ('get', b'x')]
Expected Output: [False, b'1']
Explanation: The blob is truncated after the key, so deserialization fails. The original store must remain unchanged.
Hints
- A delimiter like ':' is unsafe because keys and values may contain arbitrary bytes. Store a fixed-size length before each raw byte sequence.
- For failure recovery, do not modify the live store while parsing a blob. Decode into a temporary dictionary first, then replace the store only if parsing finishes exactly at the end.