Design a time-windowed key-value store
Problem Description
Implement a time-windowed key-value store (referred to as a "Windowed Map") that processes key/value updates at irregular intervals. The store must only retain and expose entries that fall within the most recent window of a specified length, windowMs.
Each entry in the store consists of a key, a value (which can be assumed to be a numeric type like double or long), and a timestamp (representing milliseconds since epoch, obtainable from System.currentTimeMillis() or an injected clock).
Data points with timestamps older than now - windowMs are considered expired. Expired data must be treated as invisible to users and is eligible for eviction from memory.
Assumptions & Constraints
- At any given moment, all currently valid entries are guaranteed to fit within available memory. However, the cumulative lifetime of all entries (including expired ones) will exceed memory capacity, necessitating proper expiration and eviction mechanisms.
- The system is strictly single-threaded.
API Requirements
Provide the following APIs, aiming for the highest possible efficiency:
-
Get(key):- If the
keyexists and its associated entry is not expired, return itsvalue. - Otherwise, return
null(or throw an appropriate exception). - Calling
Getmust not update the key's timestamp.
- If the
-
Put(key, value):- Inserts a new key/value pair or updates an existing one.
- Each
Putoperation must update that key’s timestamp to the currentnow.
-
GetAverage():- Return the average of all
valuesbelonging to currently valid entries within the window.
- Return the average of all
Example Scenario
Put("a", 10)att=0Put("b", 20)att=1GetAverage()returns15.0- After waiting 11 seconds (assuming a
windowMsof 10 seconds):Get("a")returnsnull(as it has expired).GetAverage()will only consider still-valid keys in its calculation.
Discussion Points
Candidates should be prepared to discuss:
- The data structures they would use to implement this system.
- The target time complexity for each API (e.g., O(1) average or worst-case), and what complexities are realistically achievable.
AI interview prep
Core dilemma
Efficiently maintaining a dynamic, time-based window of data for fast lookups, updates, and aggregations, while aggressively expiring old data to manage memory, all within a single-threaded context.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design a time-windowed key-value store
Problem Description
Implement a time-windowed key-value store (referred to as a "Windowed Map") that processes key/value updates at irregular intervals. The store must only retain and expose entries that fall within the most recent window of a specified length, windowMs.
Each entry in the store consists of a key, a value (which can be assumed to be a numeric type like double or long), and a timestamp (representing milliseconds since epoch, obtainable from System.currentTimeMillis() or an injected clock).
Data points with timestamps older than now - windowMs are considered expired. Expired data must be treated as invisible to users and is eligible for eviction from memory.
Assumptions & Constraints
- At any given moment, all currently valid entries are guaranteed to fit within available memory. However, the cumulative lifetime of all entries (including expired ones) will exceed memory capacity, necessitating proper expiration and eviction mechanisms.
- The system is strictly single-threaded.
API Requirements
Provide the following APIs, aiming for the highest possible efficiency:
-
Get(key):- If the
keyexists and its associated entry is not expired, return itsvalue. - Otherwise, return
null(or throw an appropriate exception). - Calling
Getmust not update the key's timestamp.
- If the
-
Put(key, value):- Inserts a new key/value pair or updates an existing one.
- Each
Putoperation must update that key’s timestamp to the currentnow.
-
GetAverage():- Return the average of all
valuesbelonging to currently valid entries within the window.
- Return the average of all
Example Scenario
Put("a", 10)att=0Put("b", 20)att=1GetAverage()returns15.0- After waiting 11 seconds (assuming a
windowMsof 10 seconds):Get("a")returnsnull(as it has expired).GetAverage()will only consider still-valid keys in its calculation.
Discussion Points
Candidates should be prepared to discuss:
- The data structures they would use to implement this system.
- The target time complexity for each API (e.g., O(1) average or worst-case), and what complexities are realistically achievable.