KatInterview

Key-Value Store with Expiration and Snapshots

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designhash-tablesimulationdata-structure-design

Last updated

Design a nested dictionary with field-level expiration times and the ability to capture and restore database snapshots.

Build a two-level key-value storage system that supports field expiration and snapshot management. Each operation receives a timestamp parameter (a non-decreasing integer) representing the current time.

Your task is to process a sequence of operations on this storage system. Implement a function that takes two parallel lists—one containing operation names and another containing their arguments—and returns a list of results.

Operations

The system supports five operations:

set — [timestamp, key, field, value, ttl]

  • Stores value at the location key -> field.
  • If ttl > 0, the field becomes inaccessible at time timestamp + ttl or later.
  • If ttl == 0, the field remains accessible indefinitely.
  • Overwrites any existing value and expiration for that field.
  • Returns: None

get — [timestamp, key, field]

  • Retrieves the value stored at key -> field.
  • Returns None if the key or field doesn't exist, or if the field has expired (current time is at or past the expiration time).
  • Returns: the string value or None

delete — [timestamp, key, field]

  • Removes the field at key -> field.
  • Returns True only if the field existed and had not expired at the given timestamp.
  • Returns: True or False

backup — [timestamp]

  • Captures the current database state, including only fields that have not expired at timestamp.
  • Assigns a unique integer identifier starting from 1 and incrementing with each backup.
  • Returns: the backup identifier (integer)

restore — [timestamp, backup_id]

  • Replaces the entire database with the snapshot identified by backup_id.
  • Restored fields keep their original absolute expiration times, so a field may be immediately inaccessible if its expiration time is at or before the restore timestamp.
  • Returns False if backup_id is invalid (not previously created).
  • Returns: True or False

Constraints

  • 1 <= timestamp <= 10^9
  • Timestamps are non-decreasing across all operations
  • 1 <= len(key), len(field), len(value) <= 64
  • Keys, fields, and values contain only alphanumeric characters and colons
  • 0 <= ttl <= 10^6
  • At most 20,000 total operations
  • At most 100 backup or restore operations

Examples

Example 1

Input: execute_storage_operations(['set', 'get', 'backup', 'set', 'get', 'restore', 'get', 'get'], [[1, 'user:1', 'name', 'Alice', 10], [5, 'user:1', 'name'], [6], [8, 'user:1', 'name', 'Bob', 0], [9, 'user:1', 'name'], [10, 1], [10, 'user:1', 'name'], [11, 'user:1', 'name']])

Output: [None, 'Alice', 1, None, 'Bob', True, 'Alice', None]

Hints

Hint 1

Store expiration as an absolute timestamp (timestamp + ttl) rather than a relative value, making expiration checks simple and consistent across restores.

Hint 2

When creating a backup, filter out expired fields at that moment and make deep copies so future modifications don't affect the snapshot.

Hint 3

During restore, copy the snapshot data into the active storage rather than aliasing it, preserving the snapshot for potential future restores.