Implement an Expiring Record Store
MetaSoftware Engineer · Technical ScreenLast updated
This question evaluates skills in designing and implementing an in-memory, level-based key-field-value store with lexicographic scanning, time-based versioning, TTL semantics, and efficient mutation operations.
Implement a level-based in-memory record store.
The store contains records identified by a string key. Each record contains fields, where each field is a string and each value is a string.
Level 1: Basic mutations
Implement:
set(key, field, value): Create the record if it does not exist, then setfieldtovalue.get(key, field): Return the current value forfield, ornullif the record or field does not exist.delete(key, field): Removefieldfrom the record. Returntrueif a field was removed, otherwise returnfalse. If a record becomes empty, it may be removed from the store.
Level 2: Scanning
Implement:
scan(key): Return all fields forkey, sorted lexicographically by field name. Format each item asfield(value). Return an empty list if the record does not exist.scan_by_prefix(key, prefix): Return only fields whose names start withprefix, sorted lexicographically by field name and formatted asfield(value).
Level 3: Timestamps and TTL
Extend the store so fields can be written with timestamps and optional time-to-live values.
Implement:
set_at(key, field, value, timestamp): Set the field attimestampwith no expiration.set_at_with_ttl(key, field, value, timestamp, ttl): Set the field attimestamp; it is visible for query timestsuch thattimestamp <= t < timestamp + ttl.delete_at(key, field, timestamp): Delete the field attimestampif it exists and is not expired at that time. Returntrueif a field was deleted, otherwise returnfalse.get_at(key, field, timestamp): Return the value visible attimestamp, ornullif none exists.scan_at(key, timestamp): Return all fields visible attimestamp, sorted and formatted as inscan.scan_by_prefix_at(key, prefix, timestamp): Return all visible fields attimestampwhose names start withprefix, sorted and formatted as inscan_by_prefix.
Assume timestamps passed to timestamped operations are non-decreasing. A later write to the same (key, field) replaces the previous value and expiration policy. Expired or deleted fields must not appear in reads or scans.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.