Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Thread-Safe LRU Cache with TTL
00:00
5 left

Thread-Safe LRU Cache with TTL

HardPython

Problem

Implement a class-based cache that supports get(key, now) and put(key, value, ttl, now) with capacity-limited LRU eviction and expiration. Each entry expires at time now + ttl; expired entries must behave as missing, and when capacity is full, the least recently used non-expired entry should be evicted. Design the cache so operations are thread-safe while keeping lock contention low.

Constraints

  • 1 <= capacity <= 10^5
  • At most 10^5 operations
  • key is hashable
  • 0 <= now, ttl <= 10^9
  • Expired entries must behave as absent
  • Average-case target per operation is O(1)

Function Signature

def run_lru_ttl_cache(capacity, operations):
Interviewer

Your question is Thread-Safe LRU Cache with TTL. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.