Implement an LRU Cache With Per-Entry Expiration

Last updated: November 29, 2026

Quick Overview

Implement a fixed-capacity cache supporting get and put where every entry carries its own time-to-live; expired entries must never be returned, and when the cache is full it should drop expired entries before evicting the least recently used one.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Implement a fixed-capacity cache supporting get and put where every entry carries its own time-to-live; expired entries must never be returned, and when the cache is full it should drop expired entries before evicting the least recently used one.

Microsoft onsite loops often include an object-oriented coding round where you build a small component such as the token or metadata cache that sits in front of Azure AD or Graph calls. The interviewer checks O(1) LRU mechanics, how you reason about time without sleeping in tests, and whether you notice that expiry and recency are two different orderings.

What the Interviewer Expects
  • Gets O(1) get and put for the LRU part using a hash map plus ordered list
  • Injects a clock so behavior is testable and deterministic
  • Never returns an expired value, even if it has not been evicted yet
  • Prefers evicting expired entries over live LRU entries when full
  • Handles stale heap entries after a key is overwritten
Key Topics to Cover
LRU cache
OrderedDict / doubly linked list
Min-heap
Lazy deletion
TTL
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you make this thread-safe without a single global lock becoming a bottleneck?
  • How would you add a background sweeper and what are its trade-offs versus lazy expiry?
  • How would you cap memory by bytes instead of entry count?
  • How would you avoid a thundering herd when a popular token expires?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

get(key) returns the value or None if missing or expired, and refreshes recency. put(key, value, ttl) inserts or overwrites and resets both recency and expiry. Capacity is a count of entries. When...

High-Level Design

Recency and expiry are independent orders, so I keep two structures. An OrderedDict (a hash map over a doubly linked list) gives O(1) lookup, move-to-end, and pop-oldest for LRU. A min-heap of `(exp...


Submit Your Answer
Markdown supported