771. Build an in-memory record store with scans, expiry and backups
Build a small in-memory database level by level. Each level adds operations; earlier operations must keep working.
Write run_db(ops). ops is a list of tuples whose second item is a time t; times never go down. Records are named by a key and hold fields, each with a string value. Return a list with one output per operation.
Level 1. ('set', t, key, field, value) stores the value and returns None. ('get', t, key, field) returns the value, or None if it is missing. ('delete', t, key, field) removes the field and returns True, or False if it was missing.
Level 2. ('scan', t, key, prefix) returns a list of 'field=value' strings for the fields of key whose name starts with prefix, sorted by field name. An empty prefix matches every field. A missing key gives an empty list.
Level 3. ('set_ttl', t, key, field, value, ttl) stores a value that is live only at times in [t, t + ttl). A plain set on a field removes any expiry. Every read, scan and delete treats an expired field as missing.
Level 4. ('backup', t) saves a copy of every live field together with its remaining life (expiry minus t, or no expiry) and returns how many keys have at least one live field. ('restore', t, when) replaces the whole store with the most recent backup taken at a time at most when and returns True; a restored field with remaining life r now expires at t + r. If no backup qualifies it returns False and changes nothing.
def run_db(ops)Example 1
Input: [('set', 1, 'u1', 'name', 'ann'), ('set', 2, 'u1', 'nick', 'a'), ('scan', 3, 'u1', 'n'), ('delete', 4, 'u1', 'name'), ('get', 5, 'u1', 'name')]
Output: [None, None, ['name=ann', 'nick=a'], True, None]
Example 2
Input: [('set_ttl', 10, 'k', 'f', 'v', 5), ('backup', 12), ('get', 14, 'k', 'f'), ('get', 15, 'k', 'f'), ('restore', 20, 12), ('get', 22, 'k', 'f'), ('get', 23, 'k', 'f')]
Output: [None, 1, 'v', None, True, 'v', None]
Explanation: The field had 3 ticks left at the backup, so after the restore at 20 it lives until 23.
Constraints
- At most 5,000 operations.
- ttl is a positive integer.
- Times are integers; keys, fields and values are strings.
Hint 1
Use t < expires_at, so a field set at 10 with ttl 5 dies at 15.
Hint 2
Sort scan results by field name after filtering by prefix.
Hint 3
Backups save expires_at - t; restore adds the restore time back.
Common mistakes
- Counting keys whose fields have all expired in the backup count.
- Restoring absolute expiry times, so restored fields are instantly expired.
- Letting a plain set keep an old expiry on the field.
- Returning True when deleting an expired field.
Understand the idea
Level-based assessments test whether your design absorbs new requirements cheaply. Absolute expiry times make liveness one comparison. Backups of data with a time to live need relative times, otherwise everything restored later would already be dead. Keeping helper functions small (alive, fields) means each new level adds a branch instead of rewriting old ones.
Similar questions
- Target-encode a category with smoothingHard
- Fit imputation, scaling and one-hot encoding on train and apply them to testHard
- Estimate Jaccard similarity between documents with MinHash signaturesHard
- Split folds by user so no user lands in both train and validationHard