Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Implement an LRU cache with follow-ups
LRU Cache Implementation with Concurrency Considerations
Design and implement an in-memory Least Recently Used (LRU) cache data structure.
Core Requirements
The cache must adhere to the following specifications:
- Fixed Capacity: It should have a fixed capacity
N, specified during construction. - Key-Value Pairs: Stores key-value pairs where both keys and values are integers, simplifying the implementation.
- Performance: All operations (
getandput) must achieve an average time complexity of O(1).
Operations
get(key)
- If
keyexists in the cache, return its associated value and update its status to be the most recently used. - If
keydoes not exist, return -1.
put(key, value)
- Inserts or updates the
keywith the givenvalue. - If inserting a new
keycauses the total number of keys to exceed the fixed capacityN, the least recently used key must be evicted from the cache before the new key is inserted.
Implementation & Analysis
- Describe the underlying data structures you would choose to achieve the O(1) average time complexity for both
getandputoperations. - Implement the
LRUCacheclass, including its constructor (capacity),get(key), andput(key, value)methods. - Explain the time and space complexity of your implemented solution.
Follow-up: Concurrency and Thread Safety
- How would you modify your LRU cache to ensure it is safe for use from multiple threads concurrently?
- Discuss various approaches such as coarse-grained locking, fine-grained locking, or leveraging concurrent data structures.
- Analyze the trade-offs associated with each approach, considering factors like simplicity, potential for contention, and overall performance.
- Identify specific race conditions or consistency issues that could arise if multiple threads simultaneously invoke
getandputoperations on your cache. Explain how your proposed concurrent design effectively prevents these issues.
AI interview prep
Core dilemma
Balancing the strict O(1) time complexity requirement for `get` and `put` with the need to efficiently track and update recency (LRU eviction), and then extending this to maintain correctness and performance under concurrent access.
Mid-level signals3
Senior signals3
Red flags3
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 follow-ups
LRU Cache Implementation with Concurrency Considerations
Design and implement an in-memory Least Recently Used (LRU) cache data structure.
Core Requirements
The cache must adhere to the following specifications:
- Fixed Capacity: It should have a fixed capacity
N, specified during construction. - Key-Value Pairs: Stores key-value pairs where both keys and values are integers, simplifying the implementation.
- Performance: All operations (
getandput) must achieve an average time complexity of O(1).
Operations
get(key)
- If
keyexists in the cache, return its associated value and update its status to be the most recently used. - If
keydoes not exist, return -1.
put(key, value)
- Inserts or updates the
keywith the givenvalue. - If inserting a new
keycauses the total number of keys to exceed the fixed capacityN, the least recently used key must be evicted from the cache before the new key is inserted.
Implementation & Analysis
- Describe the underlying data structures you would choose to achieve the O(1) average time complexity for both
getandputoperations. - Implement the
LRUCacheclass, including its constructor (capacity),get(key), andput(key, value)methods. - Explain the time and space complexity of your implemented solution.
Follow-up: Concurrency and Thread Safety
- How would you modify your LRU cache to ensure it is safe for use from multiple threads concurrently?
- Discuss various approaches such as coarse-grained locking, fine-grained locking, or leveraging concurrent data structures.
- Analyze the trade-offs associated with each approach, considering factors like simplicity, potential for contention, and overall performance.
- Identify specific race conditions or consistency issues that could arise if multiple threads simultaneously invoke
getandputoperations on your cache. Explain how your proposed concurrent design effectively prevents these issues.