Implement an LRU cache with O(1) ops
Problem: Implement an LRU Cache
Design and implement a Least Recently Used (LRU) cache with a fixed capacity N. The cache must support the following operations:
get(key): Retrieve the value associated withkey. Ifkeyis present, it should be marked as recently used.put(key, value): Insert or update thevalueforkey. Ifkeyalready exists, its value should be updated, and it should be marked as recently used. If the cache is at capacityNand a new key is being inserted, the least recently used item must be evicted to make space.
All get and put operations must achieve an average time complexity of O(1).
Requirements:
- Data Structures: Clearly specify the underlying data structures used for your implementation.
- Key Updates: Detail how updates to existing keys are handled, ensuring they are marked as recently used.
- Eviction Policy: Precisely define the eviction behavior when the cache capacity is exceeded.
- Thread-Safety: Discuss potential thread-safety concerns and propose solutions to ensure concurrent access is handled correctly.
- Optional TTL: Describe how an optional Time-To-Live (TTL) feature could be added for individual keys without violating the O(1) average time complexity for
getandputoperations. - Complexity Analysis: Provide a comprehensive analysis of the time and space complexity for all operations.
- Edge Cases: Identify and discuss important edge cases, such as:
- A cache with capacity
N=1. - Repeated
getoperations on the same key. - Handling large values (e.g., impact on memory).
- A cache with capacity
AI interview prep
Core dilemma
Balancing the need for O(1) average time complexity for `get` and `put` operations with the requirement to maintain a strict "least recently used" order for eviction, especially under concurrent access and with the added complexity of time-based expiry.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement an LRU cache with O(1) ops
Problem: Implement an LRU Cache
Design and implement a Least Recently Used (LRU) cache with a fixed capacity N. The cache must support the following operations:
get(key): Retrieve the value associated withkey. Ifkeyis present, it should be marked as recently used.put(key, value): Insert or update thevalueforkey. Ifkeyalready exists, its value should be updated, and it should be marked as recently used. If the cache is at capacityNand a new key is being inserted, the least recently used item must be evicted to make space.
All get and put operations must achieve an average time complexity of O(1).
Requirements:
- Data Structures: Clearly specify the underlying data structures used for your implementation.
- Key Updates: Detail how updates to existing keys are handled, ensuring they are marked as recently used.
- Eviction Policy: Precisely define the eviction behavior when the cache capacity is exceeded.
- Thread-Safety: Discuss potential thread-safety concerns and propose solutions to ensure concurrent access is handled correctly.
- Optional TTL: Describe how an optional Time-To-Live (TTL) feature could be added for individual keys without violating the O(1) average time complexity for
getandputoperations. - Complexity Analysis: Provide a comprehensive analysis of the time and space complexity for all operations.
- Edge Cases: Identify and discuss important edge cases, such as:
- A cache with capacity
N=1. - Repeated
getoperations on the same key. - Handling large values (e.g., impact on memory).
- A cache with capacity