As reported: Confluent SSE technical phone screen (USA, July 2025): implement a "Window" with a size in milliseconds that supports
get(key)andset(key, value); both operations must evict expired entries. A simple version (a queue of nodes plus a map from key to node) and a faster one were discussed. In a Confluent UK phone screen (2026) the question was described as close to LeetCode 981 (Time Based Key-Value Store) plus keeping a running sum to return the average of the values.
Implement ExpiringMap:
ExpiringMap(windowMs)— every entry lives forwindowMsmilliseconds after it was last set.put(key, value, timestamp)— setkeytovalueat timetimestamp. Setting an existing key replaces its value and restarts its lifetime.get(key, timestamp)— the value ofkeyif it is still alive attimestamp, else-1.getAverage(timestamp)— the average of the values of all entries alive attimestamp, or0when there are none.
An entry set at time t is alive at time now while now − t < windowMs. Every operation first evicts the entries that have expired at its timestamp.
Example
ExpiringMap(1000)
put("a", 10, 0)
put("b", 20, 500)
get("a", 900) -> 10
getAverage(900) -> 15.0 (a and b are alive)
get("a", 1000) -> -1 (1000 − 0 is not < 1000: "a" expired)
getAverage(1000) -> 20.0
put("b", 40, 1200) (replaces b and restarts its window)
getAverage(1499) -> 40.0
Constraints: 1 ≤ windowMs ≤ 10^9; timestamps are non-negative and non-decreasing across all calls; 0 ≤ value ≤ 10^6; keys are short strings; up to 2·10^4 operations.
Code runner format: LeetCode design format — two JSON lines, the method names (["ExpiringMap","put","get",…]) and their arguments ([[1000],["a",10,0],["a",900],…]). The runner prints the results with null for the constructor and put, and each average rounded to 5 decimals as a string ("15.00000").
Clarifications
- The average is over live entries (one value per key), not over every value ever set.
- Each operation should run in amortized O(1) time: evicting must not scan the whole map.