Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: An LRU (Least Recently Used) cache with O(1) get and put operations is classically implemented with which combination of data structures?

  1. A. A hash map (key -> node) plus a doubly linked list (to reorder/evict in O(1))
  2. B. A min-heap keyed by last-access time
  3. C. A single sorted array, binary-searched on every access
  4. D. A plain hash map alone, with no ordering structure

Correct answer: A. A hash map (key -> node) plus a doubly linked list (to reorder/evict in O(1))

The hash map gives O(1) lookup of a node by key; the doubly linked list lets that node be unlinked and moved to the front (most-recently-used end) in O(1), and the least-recently-used node (at the tail) can be evicted in O(1) too.

More Coding Interview Problems questions

Exams that ask this topic