Windowed Key-Value Store Medium · Topics · Company Tags · Hints
Design a time-windowed key-value store that retains only the entries updated within a specified duration W from the current moment. Updates can occur at arbitrary times, and the system must automatically discard entries that fall outside the window. Expired entries should be invisible to read operations and must be removed from memory to prevent unbounded growth.
You must implement the following three methods. The interviewer expects worst‑case O(1) time for each operation.
Get(key)key is present and its last update timestamp lies within the window (i.e., currentTime - timestamp <= W), return its associated value.null.Get does not alter the timestamp of the entry.Put(key, value)key → value.GetAverage()0.0.System.currentTimeMillis()) to obtain the current timestamp.W is given in the same time unit as system time (milliseconds for Java implementations).currentTime - entry.timestamp > W.Get reads without refreshing timestamps; only Put records a new timestamp.Example 1:
Input:
W = 10
Put("a", 10) at t = 0
Put("b", 20) at t = 1
GetAverage() at t = 1
Output: 15.0
Explanation: Both "a" (10) and "b" (20) are valid at t=1, so average = (10+20)/2 = 15.
Example 2:
Input:
W = 10
Put("a", 10) at t = 0
Put("b", 20) at t = 1
// wait until t = 12
Get("a") at t = 12
GetAverage() at t = 12
Output:
null
20.0
Explanation: At t=12, "a" has timestamp 0 and is expired (12-0 > 10). "b" with timestamp 1 is still valid (12-1 <= 10). GetAverage returns 20.0.
Example 3:
Input:
W = 10
GetAverage() at t = 0
Output: 0.0
Constraints:
W > 0N valid entries at any time, where N fits in standard heap memoryGet and GetAverage never see expired data