Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
LLM Response Caching
00:00
5 left

LLM Response Caching

HardPython

Problem

EPAM India's AI engineering services can reduce LLM latency and cost by reusing recent responses. Implement an in-memory response cache that supports time-based expiration and least-recently-used eviction.

Create a function that processes cache operations in chronological order. A put operation stores or replaces a response, while a get operation returns the response if it exists and has not expired. Every successful get and put marks the key as most recently used.

When inserting a new key into a full cache, remove all entries whose expiration time has passed at the operation timestamp, then evict the least recently used remaining key. Expiration uses the rule current_time >= expiration_time. A get for a missing or expired key returns None.

Formal Specification

Implement process_cache(capacity, ttl, operations), where capacity and ttl are positive integers, and operations is a list of tuples. Each tuple is either ("put", key, response, timestamp) or ("get", key, timestamp). Keys and responses are strings, and timestamps are nonnegative integers in nondecreasing order. Return a list containing the result of every get operation in order. put operations do not add an output.

Constraints

  • 1 <= capacity <= 10^4
  • 1 <= ttl <= 10^9
  • 1 <= operations.length <= 10^5
  • Each key and response is a non-empty string
  • Operation timestamps are nonnegative and nondecreasing
  • A get operation has the form ("get", key, timestamp)
  • A put operation has the form ("put", key, response, timestamp)

Function Signature

def process_cache(capacity, ttl, operations):
Interviewer

Your question is LLM Response Caching. 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.