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
Least Common Used Cache (LCU)
00:00
5 left

Least Common Used Cache (LCU)

HardPython

Problem

PayPay India services may cache frequently requested values while evicting entries that are used least often. Implement a least commonly used (LCU) cache with O(1) average-time insertion, retrieval, update, and removal.

Each cache entry has a usage frequency. A successful get and every put increase that entry's frequency by one. When the cache is full, evict the entry with the smallest frequency. If multiple entries have the same frequency, evict the least recently used entry among them.

Implement simulate_lcu(capacity, operations). Each operation is one of the following:

  1. ['put', key, value]: Insert or update an entry. Return None.
  2. ['get', key]: Return the value, or -1 if the key does not exist.
  3. ['remove', key]: Remove the entry and return True, or return False if it does not exist.

Keys are integers and values are integers. The returned list must contain one result for every operation, in order.

Constraints

  • 1 <= capacity <= 10^5
  • 1 <= len(operations) <= 2 * 10^5
  • Keys and values are integers in [-10^9, 10^9]
  • Every operation is a valid put, get, or remove command
  • All cache operations must run in O(1) average time

Function Signature

def simulate_lcu(capacity, operations):
Interviewer

Your question is Least Common Used Cache (LCU). 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.