← 返回 openai 的题目列表Implement a KV Store with Shutdown/Restore
类型:online_judge
Problem: Implement a KV Store with Shutdown/Restore
Implement a key-value store that supports the following 4 operations:
set(key, value): write string value to string key.
get(key): return the value for key; if it does not exist, return NULL (or empty string—must be defined).
shutdown(): simulate service shutdown. After shutdown, the data must be recoverable.
restore(): called on service restart to recover the state saved by the last shutdown().
Constraints & Requirements
key and value are strings.
Multiple shutdown() / restore() cycles may occur.
After restore(), the state must match exactly what it was at the last shutdown().
Explain how shutdown() persists data (e.g., write a file / serialize to disk).
Follow-up (size limit)
If the KV store has a size limit (e.g., max number of entries or max total bytes), what should happen when a set would exceed the limit?
Implement a reasonable policy (e.g., LRU eviction, LFU eviction, reject writes) and justify it.
I/O format (for evaluation)
Read a sequence of operations from stdin, execute them in order, and print one line for each get.
Line 1: integer n, number of operations
Next n lines, each is one command:
SET key value
GET key
SHUTDOWN
RESTORE
Scale
1 <= n <= 2 * 10^5
key/value length <= 100
Output
For each GET:
print the stored value if present
otherwise print NULL
Example
Input
10
SET a 1
GET a
SHUTDOWN
SET a 2
GET a
RESTORE
GET a
SET b 3
GET b
GET c
Output
1
2
1
3
NULL