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)?
- A. 81
- B. 119
- C. 78
- 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.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- The standard nextpermutation algorithm (find the rightmost i with a[i] < a[i+1], swap a[i] with the rightmost larger a[j] to its right…
- A max-heap is stored in A[1..13] = [100, 60, 90, 50, 40, 85, 80, 20, 10, 30, 35, 70, 75]. The key at position 8 (the value 20) is deleted…
- Binary search (lo = 0, hi = n − 1, mid = ⌊(lo + hi)/2⌋; each loop iteration compares a[mid] once and ends the search on equality) is run…
- Evaluate the postfix expression 8 2 3 ^ / 2 3 + 5 3 - - where ^ is exponentiation (x ^ y = xʸ), / is integer division and the operators…
- How many structurally distinct binary search trees can be built from 7 distinct keys?
- An AVL tree of height 6 (a single node has height 0) must contain at least how many nodes?