Implement LRU Cache.
HardYour program is running slowly because it's accessing data from disk over and over again. To improve the performance, you want to build a simple key-value store to cache this data in memory, but you also want to limit the amount of memory used. You decide to build a caching system that only keeps the N most recently used items—also known as a least recently used (LRU) cache.
Write a class LRUCache(n) that accepts a size limit n. It should support a set(key, value) method for inserting or updating items and a get(key) method for retrieving items. Can you implement a solution where both of these methods run in O(1) time?
Pythonclass LRUCache(n):
set(key, value)
get(key)
Examples
To make room for new items, the least recently used item should be removed. An item is 'used' whenever it is set, retrieved, or updated.
Pythoncache = LRUCache(2) # Limit of 2 items cache.set('user1', 'Alex') cache.set('user2', 'Brian') cache.set('user3', 'Chris')
Here user1 is empty because it was the least recently used and thus removed to make room for user3:
Pythoncache.get('user1') # => None cache.get('user2') # => 'Brian' cache.get('user3') # => 'Chris'
This problem is challenging because of the combination of constraints such as key-value lookup, size limits, and time complexity. Ultimately, there is no single data structure that fulfills all of these needs, so we have to get creative!
Our solution is to implement the cache class with a linked list and a hash table. The linked list is doubly-linked, which allows us to insert and remove items in constant time without traversing the entire list. Every time an item is set, updated, or retrieved, we just move its Node to the front of the list. When the cache reaches its size limit, we can easily make room by removing the item at the end of the list.
The linked list gives us O(1) insertion time, but by itself it has O(n) lookup time because we have to traverse the entire list to find a particular item. That's not ideal since we want to be able to look up an item quickly using its key. That's where the hash table comes in: we can use it add a mapping from an item's key to its Node. Together, these data structures give us a time complexity of O(1) for insertion, deletion, and retrieval in the worst case, and a space footprint of O(n).
Interview experiences
22 sharedRelated courses




























