Design a Cache system supporting get and put with configurable eviction strategies
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-10-01
Problem Statement
Design and implement a cache system that stores key-value pairs in memory, supports get and put operations, and allows configurable eviction strategies such as LRU (Least Recently Used), LFU (Least Frequently Used), or FIFO (First In First Out).
✅ Functional Requirements
1.Core Operations
•get(key) → Retrieve the value for a given key; returns null/exception if key not present.
•put(key, value) → Insert or update a key-value pair.
•size() → Return current number of items in cache.
•clear() → Remove all items from the cache.
2.Eviction Strategies
•LRU → Evict the least recently accessed item when capacity is full.
•LFU → Evict the least frequently accessed item.
•FIFO → Evict the oldest inserted item.
•Configurable at cache initialization.
3.Capacity Management
•Maximum number of items in cache should be configurable.
•Eviction triggered automatically when limit is exceeded.
4.Extensibility
•Easy to add new eviction strategies in the future.
•Support for optional expiration time per key (TTL).
🔐 Constraints & Assumptions
•Focus is on LLD and object-oriented design, not persistence to disk.
•Single-threaded or multi-threaded based on requirement; thread-safety optional.
•Keys and values can be generic types.
•Optimize for O(1) get and put operations where possible.
🧱 Key Components & Classes
1.Cache Interface / Abstract Class
1
2template <typename K, typename V>
3class Cache {
4public:
5 virtual V get(K key) = 0;
6 virtual void put(K key, V value) = 0;
7 virtual int size() = 0;
8};1.EvictionStrategy Interface
1
2template <typename K>
3class EvictionStrategy {
4public:
5 virtual void keyAccessed(K key) = 0;
6 virtual void keyAdded(K key) = 0;
7 virtual K evictKey() = 0;
8};1.Concrete Eviction Strategies
1
2class LRUCacheEviction : public EvictionStrategy<int> { ... };
3class LFUCacheEviction : public EvictionStrategy<int> { ... };
4class FIFOCacheEviction : public EvictionStrategy<int> { ... };1.Cache Implementation Class
1
2template <typename K, typename V>
3class CacheImpl : public Cache<K,V> {
4private:
5 int capacity;
6 unordered_map<K,V> cacheMap;
7 EvictionStrategy<K>* evictionPolicy;
8public:
9 V get(K key);
10 void put(K key, V value);
11};🔄 Example Workflow
1.Initialize cache with capacity 3 and LRU eviction.
2.put(1, "A"), put(2, "B"), put(3, "C") → cache full.
3.get(1) → marks key 1 as recently used.
4.put(4, "D") → eviction triggered → least recently used key 2 removed.
5.Cache now contains keys 1, 3, 4.
🧪 Expected Deliverables
•Class diagram showing Cache, EvictionStrategy, concrete eviction classes, and cache storage.
•Implementation of get, put, and eviction mechanism.
•Test cases:
•LRU eviction behavior
•LFU eviction behavior
•FIFO eviction behavior
•Capacity management
•Key updates and accesses
💡 Discussion Points
•Trade-offs: LRU vs LFU vs FIFO for different workloads.
•Extensibility for TTL (time-to-live) expiration.
•Data structures for O(1) get/put: HashMap + Doubly LinkedList (LRU), Min-Heap + HashMap (LFU).
•Thread-safety and concurrency considerations.