← 返回 bytedance 的题目列表In-Memory Key-Value Store with Snapshot and Restore
类型:online_judge
Problem: In-Memory KV Store with Snapshot and Restore
Implement an in-memory key-value store KVStore where both keys and values are strings. Support:
put(key, value): Insert or overwrite a value.
get(key): Return the value for key, or None if the key does not exist.
delete(key): Remove a key. Deleting a missing key must be a no-op.
snapshot(): Return a persistable serialized string representing the current store state.
restore(data): Restore the store from a string previously produced by snapshot(). The current state must be completely replaced.
Requirements:
Snapshot output must be deterministic: identical store states produce identical strings.
Keys and values may contain arbitrary characters, including delimiters, newlines, and Unicode. Do not use an unescaped delimiter-based format.
After restore(snapshot()), every original key-value pair must be recovered correctly.
Explain time and space complexity.
Example
put("a", "1")
put("x:y", "hello\nworld")
s = snapshot()
delete("a")
put("new", "value")
restore(s)
get("a") -> "1"
get("x:y") -> "hello\nworld"
get("new") -> None
Constraints
At most 10^5 operations.
Each key or value has length at most 10^4.
Total key/value data fits in memory.
Example
Input
put("a", "1"); put("b", "2"); snapshot(); restore(snapshot); get("a"); get("b")
Output
"1"\n"2"