Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA

Problem: An LRU cache of capacity c must support get(key) and put(key, value) in O(1) each, evicting the least recently used key. Which data structure combination achieves this?

  1. A. Min-heap keyed by last access time
  2. B. Hash map from key to node + doubly linked list ordered by recency
  3. C. Singly linked list alone
  4. D. Balanced BST keyed by last access time

Correct answer: B. Hash map from key to node + doubly linked list ordered by recency

The map finds the node in O(1); a doubly linked list moves it to the front or removes the tail in O(1) because each node knows its predecessor. A heap or BST makes every access O(log c); a singly linked list cannot unlink a node in O(1) without its predecessor and needs O(c) search.

More Coding Interview Problems questions

Exams that ask this topic