Data Structures & AlgorithmsVery hardProduct company OA
An LRU cache of capacity 3 (OrderedDict, oldest first) processes page requests. How many misses, and what is the final order?
An LRU cache of capacity 3 (OrderedDict, oldest first) processes page requests. How many misses, and what is the final order?
from collections import OrderedDict
cap, c, miss = 3, OrderedDict(), 0
for op in "A B C A D B E A C".split():
if op in c: c.move_to_end(op)
else:
miss += 1
if len(c) == cap: c.popitem(last=False)
c[op] = 1
print(miss, "".join(c))- A. 7 EAC
- B. 8 ACE
- C. 8 EAC
- D. 6 BEA
Correct answer: C. 8 EAC
A hit moves the page to the most-recent end; a miss on a full cache evicts the least recent (front). A B C: 3 misses. A: hit → B C A. D: evict B → C A D. B: evict C → A D B. E: evict A → D B E. A: evict D → B E A. C: evict B → E A C. 8 misses in 9 requests – the re-request of A just before D is what makes B, not A, the next victim. Both get and put are O(1) with a hash map + doubly linked list.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Minimum number of brackets to INSERT to make a string balanced, returned as (total, unmatched ')', unmatched '('). What is printed? python…
- Boyer–Moore majority vote WITHOUT the verification pass, returning (candidate, final counter). What is printed? python def vote(a): cand…
- Inversions counted during merge sort (pairs i < j with a[i] > a[j]). What is printed? python def inv(a): if len(a) <= 1: return a, 0 m =…
- One Hoare partition with the FIRST element (6) as pivot. What is printed (array after the partition, returned index)? python def hoare(a)…
- Counting palindromic substrings (by position) with centre expansion over 2n − 1 centres. What is printed? python def pal(s): c = 0 for…
- Sum over all subarrays of (max − min), computed as Σ max − Σ min with two monotonic-stack passes, checked against brute force. What is…