Design an LRU Cache — Concept Check
Questions on Design an LRU Cache. Implement an O(1) get/put LRU Cache using a doubly linked list and HashMap, with an optional thread-safe variant using ReadWriteLock.
Questions in this quiz
8 questions on Design an LRU Cache — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have implemented an LRU Cache with a capacity of 2. After executing the operations put(1, 'A'), put(2, 'B'), get(1), and put(3, 'C'), what is the state of the cache?
- A(1, 'A'), (2, 'B')
- B(1, 'A'), (3, 'C')
- C(2, 'B'), (3, 'C')
- D(3, 'C'), (1, 'A')
2.In a thread-safe LRU Cache using ReentrantReadWriteLock, what happens when two threads try to read simultaneously?
- ABoth threads can read concurrently
- BThe cache throws a concurrency exception
- COne thread blocks until the other finishes
- DBoth threads are blocked
3.You notice that your LRU Cache implementation is not evicting items as expected. What could be a potential issue?
- AThe addToFront() method is missing
- BThe constructor is not setting the capacity
- CThe removeTail() method is not correctly implemented
- DThe HashMap is not storing keys
4.What is the time complexity of the get() operation in a correctly implemented LRU Cache?
- AO(1)
- BO(log n)
- CO(n)
- DO(n^2)
5.A developer claims their LRU Cache implementation is slow. They are using a singly linked list instead of a doubly linked list. Why might this be a problem?
- AHashMap lookups are slower
- BNodes cannot be added to the front
- CSingly linked lists use more memory
- DRemoving nodes is not O(1) in a singly linked list
6.What happens when the put() method is called with a key that already exists in the LRU Cache?
- AThe key is ignored
- BThe cache throws an exception
- CThe cache is cleared
- DThe value is updated and the node is moved to the front
7.An LRU Cache is configured with a capacity of 3. After the sequence put(1, 'X'), put(2, 'Y'), put(3, 'Z'), get(2), put(4, 'W'), what is the least recently used item?
- A1
- B4
- C2
- D3
8.Which component in the LRU Cache ensures O(1) time complexity for moving nodes to the front?
- AReentrantReadWriteLock
- BDoubly Linked List
- CCacheStats
- DHashMap