Implement in-memory KV store with serialization
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates skills in designing and implementing an in-memory key-value store with serialization, testing data structure design, API design (set/get/serialize/deserialize), serialization format selection, type fidelity for common Python types, and robust error handling.
Constraints
- 0 <= len(operations) <= 10^4
- Store keys and all nested dict keys must be strings
- Supported stored values are int, finite float, str, list, and dict with recursive nesting
- The total size of all values and serialized blobs processed is at most 2 * 10^5
Examples
Input: [('set', 'count', 5), ('set', 'info', {'name': 'Ada', 'scores': [1, 2.5]}), ('serialize',), ('deserialize', '{"count":5,"info":{"name":"Ada","scores":[1,2.5]}}'), ('get', 'info')]
Expected Output: [True, True, '{"count":5,"info":{"name":"Ada","scores":[1,2.5]}}', True, {'name': 'Ada', 'scores': [1, 2.5]}]
Explanation: Two values are stored, the whole store is serialized to deterministic JSON, deserialized back, and the nested object is retrieved unchanged.
Input: [('set', 'x', [1, 2, 3]), ('get', 'missing'), ('deserialize', '{"x":[1,2,3]'), ('get', 'x')]
Expected Output: [True, None, False, [1, 2, 3]]
Explanation: Getting a missing key returns None. The malformed JSON blob fails to deserialize, so the previous store remains intact.
Input: [('set', 'obj', {1: 'a'}), ('serialize',), ('get', 'obj')]
Expected Output: [False, '{}', None]
Explanation: The nested dict uses a non-string key, which would lose fidelity in JSON, so set fails and the store stays empty.
Input: [('set', 'a', 1), ('deserialize', '[1,2,3]'), ('get', 'a'), ('serialize',)]
Expected Output: [True, False, 1, '{"a":1}']
Explanation: The blob is valid JSON but incompatible because the store must deserialize from a JSON object, not a list.
Input: []
Expected Output: []
Explanation: No operations means no results.
Solution
def solution(operations):
import json
import math
store = {}
def is_supported(value):
if isinstance(value, bool) or value is None:
return False
if isinstance(value, int):
return True
if isinstance(value, float):
return math.isfinite(value)
if isinstance(value, str):
return True
if isinstance(value, list):
return all(is_supported(item) for item in value)
if isinstance(value, dict):
return all(isinstance(k, str) and is_supported(v) for k, v in value.items())
return False
results = []
for op in operations:
if not op:
results.append(False)
continue
command = op[0]
if command == 'set':
if len(op) != 3:
results.append(False)
continue
key, value = op[1], op[2]
if not isinstance(key, str) or not is_supported(value):
results.append(False)
else:
store[key] = value
results.append(True)
elif command == 'get':
if len(op) != 2:
results.append(None)
continue
results.append(store.get(op[1]))
elif command == 'serialize':
if len(op) != 1:
results.append(False)
continue
results.append(json.dumps(store, sort_keys=True, separators=(',', ':'), allow_nan=False))
elif command == 'deserialize':
if len(op) != 2 or not isinstance(op[1], str):
results.append(False)
continue
try:
candidate = json.loads(op[1])
except (TypeError, ValueError):
results.append(False)
continue
if isinstance(candidate, dict) and is_supported(candidate):
store = candidate
results.append(True)
else:
results.append(False)
else:
results.append(False)
return resultsTime complexity: O(S), where S is the total size of all values and JSON blobs processed across all operations. Space complexity: O(S) for the store, parsed JSON, and recursion over nested values.
Hints
- Use a normal dictionary for the live store, and write one recursive helper that validates whether a value can be safely serialized without losing type information.
- Make serialization deterministic by sorting keys and removing extra whitespace, so exact string comparisons in tests are stable.