FastPrepLRU Cache Operation Sequence

LRU Cache Operation Sequence

Google logoGoogle● MediumFULLTIMEINTERNONSITE INTERVIEW

Problem statement

Process an operation sequence on a least-recently-used (LRU) cache with the given capacity. Each operation is an integer array. [1, key, value] means put: insert or replace that key and mark it most recently used. [0, key] means get: return its value if present and mark it most recently used; otherwise return -1 without changing recency.

When inserting a new key would exceed capacity, first evict the least recently used existing key. Replacing an existing key never evicts another key. When capacity is zero, puts store nothing and every get returns -1. Only gets produce outputs. Return their results in operation order.

The problem statement continues
Pro

Examples

Example 1

capacity = 2operations = [[1,1,10],[1,2,20],[0,1],[1,3,30],[0,2],[0,1],[0,3]]return = [10,-1,10,30]

Getting key 1 makes it newest, so inserting key 3 evicts key 2. The later gets return the remaining values.

FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 3 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
See Google hiring insights
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 3 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week