DescriptionQ142
Q142FWArchASIC interview problem
Implement a simple least-recently-used cache
TechniquesSystemVerilogLRUAssociative arrayQueue
DifficultyMedium
TopicData Structures
LanguageSystemVerilog
Requirements4 checkpoints
01
Problem
Implement a fixed-capacity least-recently-used cache. A successful lookup and every insertion or update make the key most recently used; inserting beyond capacity evicts the least-recently-used key.

Class declarationSystemVerilog
class LRUCache;
function new(int capacity);
function bit get(int key, output int value);
function void put(int key, int value);
endclassExample input and output
Use this case to check your interpretationInput
capacity=2; put(1,10), put(2,20), get(1), put(3,30)Output
get(1)=10; get(2)=MISS; keys from LRU to MRU=[1,3]Explanation
Looking up key 1 refreshes it, so the subsequent insertion evicts key 2.
02
Requirements (4)
- get(key, value) returns 0 on a miss and 1 with the stored value on a hit.
- Updating an existing key changes both its value and recency.
- Keep the least-recently-used key at the front and the most-recently-used key at the back.
- Treat a nonpositive capacity as zero, making put a no-op.
