DIY: LRU Cache
Solve the interview question "LRU Cache" in this lesson.
We'll cover the following
Problem statement
Your task is to build an LRU(least recently used) cache. A cache is great for retrieving data efficiently, but its capacity is limited. You will build a structure that initializes a cache with a fixed capacity that allows read and write operations. When the cache reaches the maximum size, replace the least recent entry with the new one.
Coding exercise
You have to implement the functions set(obj, key, value)
and get(obj, key)
. The function set(obj, key, value)
takes key
and value
along with the struct object, obj
as parameters and assigns the value against the provided key. If the key already exists, it updates the value against that key. The function get(obj, key)
takes obj
and key
as parameters and fetches the value that is stored against that key.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.