Implement an in-memory database that stores records identified by a string key. Each record contains multiple string field → string value pairs.
You must support a set of operations that progressively add features.
key
is a string.
key
maps to a set of fields.
field
maps to a
value
(both strings).
If an operation refers to a missing key or field, treat it as absent.
Assumption to make outputs well-defined (typical for OAs):
get*returns""(empty string) when absent.delete*returnstrueif something was deleted, elsefalse.scan*returns an empty list when nothing matches.
Implement:
set(key, field, value)
get(key, field) -> string
delete(key, field) -> bool
set inserts or overwrites the field’s value.
Implement:
scan(key) -> list[string]
scan_by_prefix(key, prefix) -> list[string]
Return format:
"field(value)"
.
field
.
scan_by_prefix
returns only fields whose name starts with
prefix
.
Add timestamped variants of the above operations. Tests will use either timestamped APIs or non-timestamped APIs, but never mix them.
All timestamped operations accept an integer timestamp.
Implement:
set_at(key, field, value, timestamp)
set_at_with_ttl(key, field, value, timestamp, ttl)
get_at(key, field, timestamp) -> string
delete_at(key, field, timestamp) -> bool
scan_at(key, timestamp) -> list[string]
scan_by_prefix_at(key, prefix, timestamp) -> list[string]
TTL semantics:
set_at_with_ttl
makes the field valid over the half-open interval:
[timestamp, timestamp + ttl)
get_at
,
scan_at
, or prefix scans.
Implement:
backup(timestamp)
restore(timestamp, timestamp_to_restore)
Backup requirements:
backup(t)
stores a snapshot of the database state at time
t
.
t
).
Restore requirements:
restore(now, timestamp_to_restore)
restores the database from the
latest
backup whose backup time is
≤ timestamp_to_restore
.
now
, TTL expiration must be
recalculated
based on remaining TTL stored in the backup:
r
in the backup, then after restore at time
now
it should expire at
now + r
.
Your implementation should correctly handle overwrites, deletions, scans, TTL expiry, and backup/restore interactions under the monotonic-time guarantee.