This question evaluates implementation skills in efficient in-memory data structures, time-to-live (TTL) expiration semantics, ordered and prefix scans, and snapshot backup/restore mechanisms.
Implement an in-memory database storing records by (key, field) -> value with optional TTL.
key
and
field
are strings.
value
is a string (or integer; choose one and be consistent).
(key, field)
has an expiration time
expire_at
.
expire_at = +∞
.
t
iff
t < expire_at
.
set(key, field, value, timestamp) -> void
(value, +∞)
.
set_with_ttl(key, field, value, timestamp, ttl) -> void
(value, timestamp + ttl)
.
get(key, field, timestamp) -> value | null
null
if missing or expired at
timestamp
.
delete(key, field, timestamp) -> bool
false
if
(key, field)
does not exist; otherwise
true
.
scan(key, timestamp) -> list<(field, value)>
key
at
timestamp
.
field
ascending.
scan_with_prefix(key, prefix, timestamp) -> list<(field, value)>
scan
, but only include
field
s that start with
prefix
.
backup(timestamp) -> void
timestamp
.
restore(timestamp, restore_to_time) -> void
backup_time <= restore_to_time
.