#2622
Medium JavaScript Cache with time limit
76.0% acceptance
Mar 2, 2026
531
43
Write a class that allows getting and setting key-value pairs, however a time until expiration is associated with each key.
The class has three public methods:
set(key, value, duration): accepts an integer key, an integer value, and a duration in milliseconds. Once the duration has elapsed, the key should be inaccessible. The method should return true if the same un-expired key already exists and false otherwise. Both the value and duration should be overwritten if the key already exists.
get(key): if an un-expired key exists, it should return the associated value. Otherwise it should return -1.
count(): returns the count of un-expired keys.
Solution
TypeScript
Time O(n)
Space O(1)
class TimeLimitedCache {
private cache: Map<number, { value: number; expiry: number }>;
constructor() {
this.cache = new Map();
}
set(key: number, value: number, duration: number): boolean {
const now = Date.now();
const exists = this.cache.has(key) && this.cache.get(key)!.expiry > now;
this.cache.set(key, { value, expiry: now + duration });
return exists;
}
get(key: number): number {
const now = Date.now();
const entry = this.cache.get(key);
if (entry && entry.expiry > now) return entry.value;
return -1;
}
count(): number {
const now = Date.now();
let count = 0;
for (const entry of this.cache.values()) {
if (entry.expiry > now) count++;
}
return count;
}
}
/**
* const timeLimitedCache = new TimeLimitedCache()
* timeLimitedCache.set(1, 42, 1000); // false
* timeLimitedCache.get(1) // 42
* timeLimitedCache.count() // 1
*/