Loading LLD design...
Design a Least Recently Used (LRU) Cache that supports O(1) get and put operations using a HashMap and a Doubly Linked List.
When the cache exceeds its fixed capacity, the least recently used entry is automatically evicted. Both get() and put() should mark the accessed key as most recently used. The design should cleanly separate the data structure from the eviction policy for extensibility to LFU, FIFO, etc.
Get a value by key
Return the value for a key in O(1) and mark it as most recently used; return -1 if not present
Put a key-value pair
Insert a new entry or update an existing one in O(1); if capacity is exceeded, evict the LRU entry
Evict least recently used
When the cache is full, automatically remove the entry that was accessed longest ago
Delete a key explicitly
Remove a specific key from the cache on demand
Clear the cache
Remove all entries at once
Query cache size
Return the current number of entries
Before diving into code, clarify the use cases and edge cases. Understanding the problem deeply leads to better class design.
Identify the primary actions users will perform. For a parking lot: park vehicle, exit vehicle, check availability. Each becomes a method.
Who interacts with the system? Customers, admins, automated systems? Each actor type may need different interfaces.
What are the limits? Max vehicles, supported vehicle types, payment methods. Constraints drive your data structures.
What happens on overflow? Concurrent access? Payment failures? Thinking about edge cases reveals hidden complexity.