← 返回 openai 的题目列表In-Memory KV Store with Log-Based Recovery
类型:online_judge
Implement an in-memory key-value (KV) store that can recover its state after a process interruption using a log file.
Support these operations:
SET key value: Store string value under key, replacing any existing value.
GET key: Print the value for key; print NULL if the key does not exist.
DELETE key: Remove key. Deleting a missing key is still successful.
Every state-changing operation (SET and DELETE) must be appended to a log file.
When a new KV-store instance is created, it must read and replay the existing log in order to reconstruct the latest in-memory state.
For testing, input consists of multiple lines:
LOG <log_file_path>
<command 1>
<command 2>
...
The first line specifies the log path; every remaining line is a command. Keys and values contain no whitespace. Print only the results of GET commands, one per line.
The log must represent both SET and DELETE operations correctly, and recovery must replay records in their original order. You may assume that the log directory is writable and that the log file either does not exist initially or was left by an earlier run.
Example 1
Input:
LOG /tmp/store.log
SET apple red
GET apple
DELETE apple
GET apple
Output:
red
NULL
Example 2: Recovering existing data
First run:
LOG /tmp/store.log
SET name alice
SET city seattle
Second run, using the same log file:
LOG /tmp/store.log
GET name
GET city
Output:
alice
seattle
Constraints
At most 10^5 commands.
Keys and values are each at most 10^4 characters long.
Aim for average O(1) time per operation and recovery time linear in the number of log records.
Example
Input
LOG /tmp/kv_case1.log
SET apple red
GET apple
DELETE apple
GET apple
Output
red
NULL