← 返回 openai 的题目列表Implement an In-Memory Key-Value Store with Write-Ahead Logging for Crash Recovery
类型:online_judge
Problem: Implement an In-Memory Key-Value Store with Write-Ahead Logging for Crash Recovery
Implement an in-memory key-value store that can recover its state after a crash by replaying an on-disk log file.
Requirements
Basic operations:
PUT(key, value) overwrite or create
GET(key) return value or None if missing
DELETE(key) remove a key (optional but recommended)
Persistence and recovery:
Every write operation must be appended to a log file (WAL / append-only log).
On restart, rebuild the in-memory state by replaying the log.
You must define and implement
The log record format (e.g., text lines like PUT\tkey\tvalue / DEL\tkey, or binary)
Replay ordering and overwrite semantics
How to handle special characters (tabs/newlines) inside keys/values (you may define an encoding)
Constraints
Up to 1e5 operations
Key/value length up to 1e4
If a log entry is persisted, the system should recover to the correct state after a crash
Sample Tests
First run:
PUT(a,1)
PUT(b,2)
PUT(a,3) GET(a) -> 3
After crash and restart:
Replay log GET(a) -> 3, GET(b) -> 2
Delete:
DELETE(b)
Restart then GET(b) -> None
Restart with empty log: GET(x) -> None
After many writes, restart: Recovery should be acceptable (linear replay).
Example
Input
log: (empty)
ops: GET a
Output
None