Save 25%Black Friday: 25% off all plans — use code BLACKFRIDAY2026 at checkout. Ends Friday, Dec 4.Claim the deal

(This question is a variation of LeetCode 146. LRU Cache. If you're not familiar with it, it's recommended to complete that problem first.)

Implement a data structure that processes a series of incoming key-value pairs with a timestamp, and allows querying pairs within a specified time window. Assume the minimal time unit for all operations is one minute.

Implement the WindowedKeyValueStore class:

  • WindowedKeyValueStore(int capacity, int windowSize): Initialize the cache with positive size capacity and windowSize.
  • void put(int key, int val, int time): Update the time and value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
Test Cases
Input
Output
Expected