KatInterview

Time-Aware Key-Field Store with Expiration

Data Structure DesignAuto-graded · 28 test cases
MediumAuto-gradedData Structure Designhash-mapsimulationtime-based-expiration

Last updated

Tests ability to implement an in-memory store with nested key-field structure, lexicographic scanning, timestamps, and TTL-based expiration logic.

Build an in-memory storage system that organizes data by keys and fields, supports time-based operations, and handles automatic expiration.

The store manages records indexed by string key. Each record contains multiple fields, where both field names and their value are strings.

Your implementation must process a sequence of operations and return their results.

Operations

Basic Operations

  • set(key, field, value): Store value under the given field in the record identified by key. Create the record if it doesn't exist yet. Return null.
  • get(key, field): Retrieve the current value of field in the record. Return null if the record or field doesn't exist.
  • delete(key, field): Remove field from the record. Return true if the field existed and was removed, false otherwise. Records with no remaining fields may be removed from the store.

Retrieval Operations

  • scan(key): Return all fields in the record as a list, sorted alphabetically by field name. Each entry should be formatted as "field(value)". Return an empty list if the record doesn't exist.
  • scan_by_prefix(key, prefix): Return all fields whose names begin with prefix, sorted alphabetically and formatted as "field(value)". Return an empty list if no fields match.

Time-Based Operations

Fields can be written with a specific timestamp and optional expiration time.

  • set_at(key, field, value, timestamp): Store value at the given timestamp without expiration. Return null.
  • set_at_with_ttl(key, field, value, timestamp, ttl): Store value at timestamp with a time-to-live of ttl. The field is visible for queries at time t where timestamp <= t < timestamp + ttl. Return null.
  • get_at(key, field, timestamp): Retrieve the value visible at timestamp. Return null if no valid value exists at that time.
  • delete_at(key, field, timestamp): Remove field if it exists and is visible at timestamp. Return true if deleted, false otherwise.
  • scan_at(key, timestamp): Return all fields visible at timestamp, sorted and formatted as in scan.
  • scan_by_prefix_at(key, prefix, timestamp): Return all fields visible at timestamp whose names start with prefix, sorted and formatted as in scan_by_prefix.

Behavior Rules

  • Timestamps in time-based operations are guaranteed to be non-decreasing.
  • A later write to the same (key, field) pair completely replaces the previous value and its expiration policy.
  • Fields that have expired or been deleted must not appear in any retrieval operations.
  • A field written with set_at_with_ttl expires at exactly timestamp + ttl (not visible at or after this time).
  • Empty prefix "" matches all field names.

Input Format

You will receive two parallel lists:

  • operations: list of operation names (strings)
  • arguments: list of argument lists, one per operation

Return a list containing one result per operation, in order. Mutations that don't return a value should produce None in the results.

Constraints

  • 1 ≤ number of operations ≤ 10,000
  • All keys, fields, and values are non-empty strings
  • All timestamps and TTL values are non-negative integers
  • Timestamps are non-decreasing across operations

Examples

Example 1

Input: time_aware_store(['set', 'get', 'delete', 'get'], [['user1', 'name', 'Alice'], ['user1', 'name'], ['user1', 'name'], ['user1', 'name']])

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

Example 2

Input: time_aware_store(['set', 'set', 'set', 'scan', 'scan_by_prefix', 'get'], [['user1', 'name', 'Alice'], ['user1', 'age', '30'], ['user1', 'nickname', 'Al'], ['user1'], ['user1', 'na'], ['user1', 'age']])

Output: [None, None, None, ['age(30)', 'name(Alice)', 'nickname(Al)'], ['name(Alice)'], '30']

Example 3

Input: time_aware_store(['set_at_with_ttl', 'get_at', 'get_at', 'scan_at'], [['k', 'f', 'v', 10, 5], ['k', 'f', 12], ['k', 'f', 15], ['k', 14]])

Output: [None, 'v', None, ['f(v)']]

Example 4

Input: time_aware_store(['set_at', 'set_at_with_ttl', 'get_at', 'get_at', 'delete_at', 'get_at'], [['k', 'f', 'old', 1], ['k', 'f', 'new', 5, 3], ['k', 'f', 6], ['k', 'f', 8], ['k', 'f', 8], ['k', 'f', 9]])

Output: [None, None, 'new', None, False, None]

Hints

Hint 1

Use nested dictionaries to map keys to fields, and store both value and expiration information for each field.

Hint 2

For TTL tracking, store when each field expires rather than when it was created. Check expiration at query time rather than actively removing expired entries.

Hint 3

A single helper function can handle all scan operations by accepting a timestamp and optional prefix, filtering by visibility and prefix match, then sorting.