Movie LRU Cache
Updated
Problem statement
A movie cache holds at most capacity distinct movie names. Process the movie names in accesses from left to right.
- Accessing a cached movie makes it most recently used.
- Accessing a new movie inserts it as most recently used.
- If insertion exceeds capacity, evict the least recently used movie.
Return the names remaining in the cache from most recently used to least recently used. If capacity is zero, return an empty array.
Function
recentMovies(capacity: int, accesses: String[]) → String[]Examples
Example 1
capacity = 3accesses = ["Arrival","Heat","Up","Heat","Jaws"]return = ["Jaws","Heat","Up"]Heat is refreshed before Jaws evicts the least-recent Arrival entry.
Example 2
capacity = 2accesses = ["A","B","A"]return = ["A","B"]The final access moves A ahead of B.
Constraints
0 <= capacity <= 100000.0 <= accesses.length <= 200000.- Every movie name is non-empty.
More Grab Phone Screen questions
Similar questions at other companies
- LRU CacheAthenahealth · PHONE SCREEN
- 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 Key-Value Cache OperationsCohesity · PHONE SCREEN
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Least Recently Used CacheByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- LRU CacheCoupang · PHONE SCREEN
- LRU CacheBlinkit · PHONE SCREENBlinkit interview questions
- LRU Cache with Thread-Safety Follow-UpSnap Inc. · PHONE SCREENSnap Inc. interview questions