Skip to question
ASIC.FYI
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
ASIC.FYI/Interview questions/Make the least-recently-used cache O(1)

Q143·Free·Computer Architecture

Make the least-recently-used cache O(1)

Difficulty
Hard
Topic
Data Structures
Language
SV
Interview prompt

Question

Redesign the LRU cache so successful get, put, recency updates, and eviction all take average O(1) time.

Key-to-node map beside a doubly linked LRU-to-MRU list before and after touching one key
Direct node references allow a touched key to move to the MRU end without scanning.
Starting point

Question code

class LRUCacheO1;
  function new(int capacity);
  function bit get(int key, output int value);
  function void put(int key, int value);
endclass
Reviewed example

Trace one case

Input
capacity=2; put(1,10), put(2,20), get(1), put(3,30)
Expected output
key 2 is evicted; node-map entries are {1,3}; LRU-to-MRU list is [1,3]

Direct node lookup and constant-time unlink/append preserve the same LRU behavior without a scan.

What to cover

Requirements

  1. Preserve the lookup, update, insertion, and eviction behavior of the simple LRU cache.
  2. Map each key directly to its recency node.
  3. Unlink and append a node without scanning the cache.
  4. Evict the head/LRU key from the value map, node map, and linked list together.
Exact question handoffPractice Q143

Solve it in the question bank, keep your progress, and reveal the reviewed solution when your access allows.

Open in question bank →
Solution accessA Free account unlocks the complete reviewed solution.
Continue learning

Computer Architecture

  • Data Structures
  • SystemVerilog
  • LRU
  • Doubly linked list
Firmware interview questions →
Continue practicing

Related questions

Q142 · Data StructuresImplement a simple least-recently-used cache→Q089 · Data StructuresImplement an O(1) LRU cache→Q218 · ArraysMerge inclusive overlapping intervals→
ASIC.FYI · Learn silicon end to end.info@asic.fyi