← 返回 instacart 的题目列表In-Memory Database with Backup and Restore
类型:online_judge
Problem: In-Memory Database with Backup and Restore
Design and implement a simplified in-memory database that supports read/write on key-values and also supports backup and restore.
Data Model
Each record is identified by a unique string key.
Each record contains multiple fields, where each field name is a string field.
Each field maps to an integer value.
Operations (processed in order)
Process a sequence of commands and output results for specific commands.
SET key field value
Set field under key to integer value.
Create key or field if they do not exist.
No output.
GET key field
If the value exists, print the integer; otherwise print NULL.
DELETE key field
Remove field under key.
If key has no remaining fields, remove the key as well.
No output.
BACKUP backup_id
Save a full snapshot of the current database state under string id backup_id.
If backup_id already exists, overwrite it.
No output.
RESTORE backup_id
Restore the database state to the snapshot identified by backup_id.
If backup_id does not exist, do nothing.
No output.
I/O Format
First line: integer Q (#commands).
Next Q lines: one command per line.
Print one line per GET command.
Constraints
1 <= Q <= 2 * 10^5
key/field/backup_id contain lowercase letters and digits, length 1..20
value fits in 32-bit signed int
Should run efficiently (near O(Q) or O(Q log Q)).
Example
Input
10
SET a x 1
GET a x
BACKUP b1
SET a x 2
GET a x
RESTORE b1
GET a x
DELETE a x
GET a x
RESTORE b2
Output
1
2
1
NULL