Implement an in-memory storage with TTL and scans
In-Memory Key-Field-Value Store with TTL and Time-Travel
Implement an in-memory storage system that functions as a tiny key-field-value database.
Data Model
The data model is a 2-level map: key -> field -> value.
Each operation is associated with a timestamp ts, which is guaranteed to be non-decreasing across the sequence of input queries.
Individual (key, field) entries can optionally have a Time-To-Live (TTL). An entry is considered expired after its TTL elapses and must behave as if it no longer exists.
You will be provided with a sequence of commands to process in order. For commands that produce an output, collect and return these outputs.
Commands
Assume all key, field, value, and prefix strings are non-empty and contain no spaces. ts and ttl are integers.
Basic Operations
SET ts key field value- Sets the
valuefor the(key, field)pair at timets. - This write has no TTL (it never expires) and overwrites any existing value or TTL for that
(key, field)pair.
- Sets the
GET ts key field- Returns the current value of
(key, field)at timets. - If the entry is missing or expired at
ts, an empty string""should be returned.
- Returns the current value of
COMPARE_AND_SET ts key field expected newValue- If the current (non-expired) value of
(key, field)at timetsequalsexpected, the value is updated tonewValue. This new value has no TTL. - Returns
"true"if the update occurred, otherwise"false".
- If the current (non-expired) value of
COMPARE_AND_DELETE ts key field expected- If the current (non-expired) value of
(key, field)at timetsequalsexpected, the field is deleted. - Returns
"true"if the deletion occurred, otherwise"false".
- If the current (non-expired) value of
Scan Operations
SCAN ts key- Returns all non-expired fields for the given
keyat timets. - The fields must be sorted lexicographically by their field names.
- The output format for each field-value pair is
field(value), joined by commas (e.g.,a(1),b(2)). - If
keyhas no non-expired fields, return"".
- Returns all non-expired fields for the given
SCAN_BY_PREFIX ts key prefix- Identical to
SCAN, but only includes fields whose names start with the specifiedprefix.
- Identical to
TTL Support
SET_WITH_TTL ts key field value ttl- Sets the
valuefor(key, field)at timetswith an expiration timeexpireAt = ts + ttl. - The entry is considered valid for any timestamp
tsuch thatts <= t < expireAt. - This operation overwrites any previous value or TTL for the
(key, field)pair.
- Sets the
TTL Rule: At any given timestamp ts, an entry is visible only if it exists and is not expired at ts.
Time-Travel Queries (Advanced)
GET_AT ts key field atTs- Returns the value that
(key, field)had at a specific historical timestampatTs. - TTL rules and deletions are respected for
atTs. - If the entry did not exist or was expired at
atTs, return"". - It is guaranteed that
atTs <= ts.
- Returns the value that
Input and Output
- Input:
queries, a list where each element is a list of strings representing a single command (tokens in the order shown above). - Output: A list of strings containing the results of each command that produces output.
- Commands producing output:
GET,COMPARE_AND_SET,COMPARE_AND_DELETE,SCAN,SCAN_BY_PREFIX,GET_AT. SETandSET_WITH_TTLproduce no output.
- Commands producing output:
Constraints
- The number of queries
len(queries)is between 1 and 100,000. - Timestamps are strictly non-decreasing.
- The total length of all strings involved is manageable within typical memory limits.
Implement a function that processes the given list of queries and returns the corresponding list of outputs.
AI interview prep
Core dilemma
The central engineering conflict is balancing efficient current-state operations (SET, GET, SCAN) with the complex requirement of maintaining and querying historical states for time-travel (`GET_AT`) and correctly applying TTL expirations across different timestamps without excessive memory or computational overhead.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement an in-memory storage with TTL and scans
In-Memory Key-Field-Value Store with TTL and Time-Travel
Implement an in-memory storage system that functions as a tiny key-field-value database.
Data Model
The data model is a 2-level map: key -> field -> value.
Each operation is associated with a timestamp ts, which is guaranteed to be non-decreasing across the sequence of input queries.
Individual (key, field) entries can optionally have a Time-To-Live (TTL). An entry is considered expired after its TTL elapses and must behave as if it no longer exists.
You will be provided with a sequence of commands to process in order. For commands that produce an output, collect and return these outputs.
Commands
Assume all key, field, value, and prefix strings are non-empty and contain no spaces. ts and ttl are integers.
Basic Operations
SET ts key field value- Sets the
valuefor the(key, field)pair at timets. - This write has no TTL (it never expires) and overwrites any existing value or TTL for that
(key, field)pair.
- Sets the
GET ts key field- Returns the current value of
(key, field)at timets. - If the entry is missing or expired at
ts, an empty string""should be returned.
- Returns the current value of
COMPARE_AND_SET ts key field expected newValue- If the current (non-expired) value of
(key, field)at timetsequalsexpected, the value is updated tonewValue. This new value has no TTL. - Returns
"true"if the update occurred, otherwise"false".
- If the current (non-expired) value of
COMPARE_AND_DELETE ts key field expected- If the current (non-expired) value of
(key, field)at timetsequalsexpected, the field is deleted. - Returns
"true"if the deletion occurred, otherwise"false".
- If the current (non-expired) value of
Scan Operations
SCAN ts key- Returns all non-expired fields for the given
keyat timets. - The fields must be sorted lexicographically by their field names.
- The output format for each field-value pair is
field(value), joined by commas (e.g.,a(1),b(2)). - If
keyhas no non-expired fields, return"".
- Returns all non-expired fields for the given
SCAN_BY_PREFIX ts key prefix- Identical to
SCAN, but only includes fields whose names start with the specifiedprefix.
- Identical to
TTL Support
SET_WITH_TTL ts key field value ttl- Sets the
valuefor(key, field)at timetswith an expiration timeexpireAt = ts + ttl. - The entry is considered valid for any timestamp
tsuch thatts <= t < expireAt. - This operation overwrites any previous value or TTL for the
(key, field)pair.
- Sets the
TTL Rule: At any given timestamp ts, an entry is visible only if it exists and is not expired at ts.
Time-Travel Queries (Advanced)
GET_AT ts key field atTs- Returns the value that
(key, field)had at a specific historical timestampatTs. - TTL rules and deletions are respected for
atTs. - If the entry did not exist or was expired at
atTs, return"". - It is guaranteed that
atTs <= ts.
- Returns the value that
Input and Output
- Input:
queries, a list where each element is a list of strings representing a single command (tokens in the order shown above). - Output: A list of strings containing the results of each command that produces output.
- Commands producing output:
GET,COMPARE_AND_SET,COMPARE_AND_DELETE,SCAN,SCAN_BY_PREFIX,GET_AT. SETandSET_WITH_TTLproduce no output.
- Commands producing output:
Constraints
- The number of queries
len(queries)is between 1 and 100,000. - Timestamps are strictly non-decreasing.
- The total length of all strings involved is manageable within typical memory limits.
Implement a function that processes the given list of queries and returns the corresponding list of outputs.