Design time-based key-value store
Design Time-Based Key-Value Store
Design an in-memory key-value store that supports time-based lookups.
Operations
Implement a class or module with the following methods:
-
set(key, value, timestamp)- Stores the string
valuefor the stringkeyat the integertimestamp. - Multiple values may be stored for the same key, each associated with a different timestamp.
- Stores the string
-
get(key, timestamp)- Returns the value associated with
keyfor the largest stored timestamptsuch thatt <= timestamp. - If no such timestamp exists for the given key, return an empty string. Ensure consistency in the return type (e.g., always empty string or always
null).
- Returns the value associated with
Notes and Constraints
- Timestamps for
set()calls on the same key are guaranteed to be non-decreasing. - The primary goal is to achieve efficient
get()query performance. - Keys and values are ASCII strings.
- The store is entirely in-memory.
Example
Consider the following sequence of operations:
set("room", "A", 10)set("room", "B", 20)get("room", 5) -> ""get("room", 10) -> "A"get("room", 15) -> "A"get("room", 20) -> "B"get("room", 25) -> "B"
AI interview prep
Core dilemma
Balancing efficient logarithmic-time retrieval of historical values (largest timestamp <= query timestamp) with potentially high-volume, ordered write operations, all within strict in-memory constraints.
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design time-based key-value store
Design Time-Based Key-Value Store
Design an in-memory key-value store that supports time-based lookups.
Operations
Implement a class or module with the following methods:
-
set(key, value, timestamp)- Stores the string
valuefor the stringkeyat the integertimestamp. - Multiple values may be stored for the same key, each associated with a different timestamp.
- Stores the string
-
get(key, timestamp)- Returns the value associated with
keyfor the largest stored timestamptsuch thatt <= timestamp. - If no such timestamp exists for the given key, return an empty string. Ensure consistency in the return type (e.g., always empty string or always
null).
- Returns the value associated with
Notes and Constraints
- Timestamps for
set()calls on the same key are guaranteed to be non-decreasing. - The primary goal is to achieve efficient
get()query performance. - Keys and values are ASCII strings.
- The store is entirely in-memory.
Example
Consider the following sequence of operations:
set("room", "A", 10)set("room", "B", 20)get("room", 5) -> ""get("room", 10) -> "A"get("room", 15) -> "A"get("room", 20) -> "B"get("room", 25) -> "B"