Daily iOS
CS fundamentals and algorithms

LRU cache with TTL

You've implemented an LRU cache and the interviewer follows up: "Now make each entry expire a fixed time after it's stored. What gets more expensive?" Explain the cheapest way to check expiry and what that approach costs you.

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Store an expiry time on each node and check lazily, only on the entry you touch in get/put, removing it if expired, which keeps O(1) but lets expired entries take up capacity and skew which entries LRU evicts. In practice, lazy expiry plus a low-frequency sweep.

Share a question