Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Thread-Safe LLM Inference Cache

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

Your question is Thread-Safe LLM Inference Cache. 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 thread-safe least recently used cache for frequently repeated Intone Networks LLM inference results. The cache stores string keys and arbitrary result values, returns cached results, and evicts the least recently accessed entry when capacity is exceeded.

Your function receives a capacity and a sequence of operations. Each operation is either ['set', key, value] or ['get', key]. Return the values produced by get operations in order. Return None for a cache miss. A set operation marks the key as most recently used, whether the key is new or already exists.

The implementation must protect shared state with a synchronization primitive so simultaneous get and set calls cannot corrupt the cache. Both successful lookup and update, including eviction, should run in average O(1) time.

Formal Specification

  • Input: capacity, a positive integer, and operations, a list of operations. Keys are strings, and values may be any Python object.
  • Output: A list containing one value for each get operation, in operation order.
  • Thread safety: Cache state must be protected while reading, updating recency, inserting, and evicting entries.

Constraints

  • 1 <= capacity <= 10^5
  • 1 <= len(operations) <= 2 * 10^5
  • Each operation is either ['get', key] or ['set', key, value]
  • Keys are strings
  • Values may be any Python object

Function Signature

def thread_safe_cache(capacity, operations):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output