
You are asked to implement a small in-memory “database” that evolves across 4 parts. In each part you may reuse your previous code and only add/extend methods.
Each record is identified by a string key and stores a string value.
Time is represented by an integer timestamp t (monotonically increasing in tests).
Implement a database supporting:
put(key, value, t)
: store
value
for
key
.
get(key, t) -> value | null
: return the currently stored value for
key
, or
null
if missing.
Notes:
put
is called multiple times for the same key, the latest write should be returned.
Add:
scan(prefix, t) -> List<(key, value)>
: return all key/value pairs whose
key starts with prefix
.
Requirements:
Extend put to optionally accept a TTL:
put(key, value, t, ttlSeconds)
means the entry is valid for timestamps in the half-open interval
[t, t + ttlSeconds)
. After that it is expired.
Update get and scan so that expired items are not returned.
Clarifications:
put
, the new value/TTL replaces the old one.
Add support for backups:
backup(t) -> backupId
: captures the database state at time
t
(only non-expired entries at time
t
).
restore(backupId, t)
: restores the database to exactly the state captured in that backup.
TTL behavior on restore:
Implement the required methods so that all provided test cases pass.