GATE Computer ScienceVery hardGATE CS

An LRU cache of capacity 3 (get and put both make a key the most recently used; put of an existing key updates its value; when a put adds a fourth key the least recently used key is evicted; get returns −1 on a miss). Operations: put(1,10), put(2,20), put(3,30), get(1), put(4,40), get(2), put(5,50), get(3), get(1), put(1,11), put(6,60), get(4), get(1), get(5). What is the sum of the values returned by all get operations (a miss counts as −1)?

  1. A. 81
  2. B. 119
  3. C. 78
  4. D. 85

Correct answer: C. 78

Cache order (LRU to MRU): after the first three puts [1,2,3]; get(1)=10 gives [2,3,1]; put(4) evicts 2; get(2) = −1; put(5) evicts 3; get(3) = −1; get(1) = 10; put(1,11); put(6) evicts 4; get(4) = −1; get(1) = 11; get(5) = 50. Sum 10 − 1 − 1 + 10 − 1 + 11 + 50 = 78. A FIFO cache would give 119.

More Programming and Data Structures (GATE) questions

Exams that ask this topic