Problem, as an interviewer would state it: "Wrap localStorage so each stored item has a TTL (time to live). Reading an expired item should return null and actually clean it up, not just report it as missing while leaving stale data sitting in storage forever."
Examples:
setWithTTL("session", "abc123", 100); // expires in 100ms
getWithTTL("session"); // "abc123" (still valid)
// after 150ms...
getWithTTL("session"); // null, AND the raw entry is genuinely removed from storage
Clarifying questions expected:
- Should expired entries be cleaned up lazily (only when read) or proactively (a background sweep)?
- What should happen if a stored value isn't valid JSON (corrupted or from an older, incompatible format)?
- Is a global default TTL needed, or is TTL always specified per item?
Code / implementation expected: Yes — real, live-verified proof: a value reads correctly before expiry, reads as null after expiry, and the underlying raw localStorage entry is genuinely removed as a side effect of that expired read.