Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Thread-Safe LRU Cache with TTL

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Thread-Safe LRU Cache with TTL. Start with the requirements on the right.

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.

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output