Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: An LRU cache of capacity 2 handles requests 1, 2, 1, 3, 2, 1, 4. Each request that is not cached causes a miss and evicts the least recently used item if the cache is full. How many misses occur?

  1. A. 5
  2. B. 4
  3. C. 6
  4. D. 7

Correct answer: C. 6

LRU: 1 m, 2 m, 1 h, 3 m (evict 2), 2 m (evict 1), 1 m (evict 3), 4 m (evict 2): 6 misses. FIFO (no move on hit) gives 5, counting distinct keys gives 4, and 7 treats every request as a miss.

More Coding Interview Problems questions

Exams that ask this topic