r/csinterviewproblems • • Dec 17 '15

[Algorithm Design] Implement an LRU cache

Implement a cache that uses a least-recently-used algorithm to evict data when necessary. The cache can be initialized with a max size, and should support "get", "set", and "delete" operations.

get(key) //Returns value for "key"

set(key, value) //Sets value for "key"

delete(key) //Removes "key"

An item is "used" when a get or set is performed. When the cache is full (size == maxSize) and an insertion is performed, the least recently used item should be evicted.

6 Upvotes

2 comments sorted by

View all comments

Show parent comment

1

u/fp_ Dec 20 '15

Hi, a question since I'm not overly familiar with JavaScript (which this looks like):

I don't see the tail being set anywhere, notably when adding the first element in in set - a check that is not included in the set code. This seems like it would make popTail() always return null since the tail was never changed. Am I missing something?