0
0
Data Structures Theoryknowledge~30 mins

LRU cache design with hash map and doubly linked list in Data Structures Theory - Mini Project: Build & Apply

Choose your learning style9 modes available
LRU Cache Design with Hash Map and Doubly Linked List
📖 Scenario: You are building a system that remembers the most recently used items to speed up access. This system should forget the least recently used items when it runs out of space.To do this efficiently, you will combine a hash map for quick lookups and a doubly linked list to track usage order.
🎯 Goal: Build a simple LRU (Least Recently Used) cache that supports adding, retrieving, and automatically removing the least recently used items when full.
📋 What You'll Learn
Create a doubly linked list node class with key, value, prev, and next pointers
Create a hash map (dictionary) to store keys and their corresponding nodes
Implement methods to add nodes to the front and remove nodes from the list
Implement the LRU cache with get and put methods that update usage order and remove least recently used items when capacity is exceeded
💡 Why This Matters
🌍 Real World
LRU caches are used in web browsers, databases, and operating systems to speed up access to frequently used data by remembering recent items and discarding older ones.
💼 Career
Understanding LRU cache design is important for software engineers working on performance optimization, system design, and memory management in real-world applications.
Progress0 / 4 steps
1
Create the Node class for the doubly linked list
Define a class called Node with an __init__ method that takes key and value as parameters. Initialize self.key, self.value, self.prev, and self.next attributes. Set self.prev and self.next to None.
Data Structures Theory
Need a hint?

Think of each node as a box holding a key and value, with links to the previous and next boxes.

2
Set up the LRUCache class with capacity and initial data structures
Create a class called LRUCache with an __init__ method that takes capacity as a parameter. Inside, create a dictionary called cache to store keys and nodes. Initialize self.capacity with the given capacity. Also, create two dummy nodes called head and tail of type Node with key and value set to 0. Link head.next to tail and tail.prev to head.
Data Structures Theory
Need a hint?

The dummy head and tail nodes help manage the doubly linked list easily without checking for empty states.

3
Add helper methods to add and remove nodes from the doubly linked list
Inside the LRUCache class, define two methods: _add_node(self, node) and _remove_node(self, node). The _add_node method should insert the given node right after head. The _remove_node method should unlink the given node from the list by adjusting the prev and next pointers of its neighbors.
Data Structures Theory
Need a hint?

Adding a node means placing it right after the head. Removing means skipping over it by linking its neighbors.

4
Implement get and put methods for the LRUCache
Inside the LRUCache class, implement the get(self, key) method that returns the value of the node with the given key if it exists, otherwise returns -1. When accessed, move the node to the front (most recently used). Also implement the put(self, key, value) method that adds a new node or updates an existing node's value, moves it to the front, and if the cache exceeds capacity, removes the least recently used node from the end.
Data Structures Theory
Need a hint?

When you get a key, move it to the front. When you put a key, add or update it and remove the least used if full.