java - How can you implement LFU cache using simplest and minimum data structures.? -
this question has answer here:
- what difference between lru , lfu 3 answers
i asked question in interview asked first difference between lru , lfu , asked implement both. knew lru can implemented through linkedhashmap got confused lfu. can tell me how implement simplest data structures explaination? can implemented linkedhashmap too.?
assuming cache entries keyed, queue (linkedlist
) , map (hashmap
).
(assume queue , map full)
pick bound b
queue. on every cache request, push key requested page queue poll queue.
if page want in map, return page. if page isn't in map find least occurring key in queue in map or key page want. if key key page want, nothing; else remove entry map key , insert page map.
return page.
complexity cache hit o(1), o(b) miss.
assumes want bound frequency. ie. "least used in last b requests" instead of "least used ever".
Comments
Post a Comment