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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Implementing an LRU Cache
00:00
5 left

Implementing an LRU Cache

HardPython

Problem

Code out an LRU cache.

Implement lru_cache(operations, capacity), where each operation is a list of strings and integers. A put operation has the form ["put", key, value]; a get operation has the form ["get", key]. Return the values produced by get operations in order, using -1 for missing keys. Both get and put must run in O(1) average time. Accessing or updating a key makes it the most recently used item. When the cache exceeds capacity, evict the least recently used item.

Constraints

  • 1 <= capacity <= 500
  • 1 <= operations.length <= 500
  • Each operation is either ["get", key] or ["put", key, value]
  • 0 <= key <= 10^9
  • 0 <= value <= 10^9
  • A get operation returns -1 when its key is absent

Function Signature

def lru_cache(operations, capacity):
Interviewer

Your question is Implementing an LRU Cache. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.