Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.