21
How would you implement an in-memory cache with expiration?
Tap to write answer
0 words | 0 charsPress Enter ↵ to reveal
Your Attempt
0 wordsRefined Model Answer
ReferenceI would use a hash map for fast lookup and store an expiration time with each entry. On access, I would check whether the item has expired and remove it if necessary, and I would also need a background cleanup strategy if the cache grows large. The reason I choose this design is that it keeps reads fast while still enforcing freshness. If eviction policy matters, I could combine expiration with LRU logic. I would also mention edge cases like clock changes, zero TTL, and handling expired entries that have not yet been cleaned up.