Problem, as an interviewer would state it:
"Design a cache where set(key, value, duration) stores a value that expires after duration milliseconds. set returns true if an un-expired entry for that key already existed, otherwise false."
Examples:
cache.set(1, 42, 100); // false (new key)
cache.set(1, 50, 50); // true (an unexpired entry existed; value and TTL overwritten)
cache.get(1); // 50
Clarifying questions expected:
- What should
getreturn for a missing or expired key? - Does overwriting reset the expiry, and is it the NEW duration that applies?
- Should there be a way to count the currently valid keys?
Code / implementation expected: Yes — real, direct proof of the expiry boundary, the overwrite return value, and cleanup, ideally testable without real waiting.