Data Structures & AlgorithmsHardAmazon · Microsoft OA

An LRU cache must support get and put in O(1). Which design achieves this?

  1. A. A min-heap keyed by last-access time
  2. B. A hash map to nodes of a doubly linked list kept in recency order
  3. C. A sorted array of (time, key) pairs
  4. D. A singly linked list with a hash map from key to node

Correct answer: B. A hash map to nodes of a doubly linked list kept in recency order

The map finds a node in O(1); the doubly linked list removes and reinserts it in O(1) because each node knows its predecessor. A heap needs O(log n) updates, and a singly linked list cannot unlink a node without its predecessor.

More Coding Interview Problems questions

Exams that ask this topic