Meta LRU Cache Operations
Updated
Problem statement
Process an ordered sequence of operations on a least recently used (LRU) cache with the given capacity. Return the answers from all get and last operations in execution order.
[1, key, value]— Put: insert or replace the value and mark the key most recently used. Inserting a new key into a full cache evicts the least recently used key. Updating an existing key does not evict another key.[0, key]— Get: return the stored value, or-1if the key is absent. A successful get marks the key most recently used; a miss does not change recency.[2, key]— Delete: remove the key if present. An absent key is a no-op. This operation produces no answer and does not reorder the remaining keys.[3]— Last: return the most recently used key currently in the cache, or-1when empty. Return the key, not its value. This operation does not change recency.
A cache with capacity zero never retains an entry. Put and delete produce no output; include only get and last answers in the returned integer array.
Function
runLruCache(capacity: int, operations: int[][]) → int[]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.
Example 2
capacity = 2operations = [[1,1,10],[1,2,20],[1,1,15],[1,3,30],[0,2],[0,1]]return = [-1,15]Updating key 1 marks it newest without evicting. Inserting key 3 evicts key 2, and key 1 retains its replacement value.
Example 3
capacity = 0operations = [[1,7,70],[0,7],[1,8,80],[0,8]]return = [-1,-1]A zero-capacity cache never retains a put value.
Example 4
capacity = 1operations = [[1,4,40],[0,9],[1,5,50],[0,4],[0,5]]return = [-1,-1,50]The miss for key 9 does not affect recency. Inserting key 5 evicts the only stored key.
Example 5
capacity = 2operations = [[3],[1,7,70],[1,2,20],[3],[0,7],[3],[2,7],[3],[2,2],[3]]return = [-1,2,70,7,2,-1]The empty cache returns -1. Last first returns key 2, then key 7 after the successful get. Deleting key 7 exposes key 2 as newest. Deleting key 2 empties the cache.
Constraints
0 <= capacity <= 10000.1 <= operations.length <= 10000.- Every operation is exactly
[0, key],[1, key, value],[2, key], or[3]. 0 <= key, value <= 10^9.
More Meta Onsite Interview questions
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- LRU CacheLotusFlare · ONSITE INTERVIEW
- LRU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- LRU Cache OperationsHevo Data · ONSITE INTERVIEW
- LRU Cache OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- LRU Cache Operation SequenceGoogle · ONSITE INTERVIEWGoogle interview questions
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- LRU CacheAMD · ONSITE INTERVIEWAMD interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences