West Midlands | 26 March SDC | Iswat Bello | Sprint 2 | Implement an LRU cache in python#193
Open
Iswanna wants to merge 10 commits into
Open
West Midlands | 26 March SDC | Iswat Bello | Sprint 2 | Implement an LRU cache in python#193Iswanna wants to merge 10 commits into
Iswanna wants to merge 10 commits into
Conversation
- Add 'key' attribute to Node to facilitate dictionary cleanup during eviction - Maintain pointers for doubly linked list functionality
- Initialize head and tail pointers for the Doubly Linked List - Add documentation explaining MRU (head) and LRU (tail) logic
- Add logic to insert nodes at the front of the list (Most Recently Used) - Ensure tail pointer is set correctly when adding to an empty list - Update previous/next pointers of existing head to maintain bi-directional links
- Add logic to remove and return the least recently used (tail) node - Handle single-node list resets by clearing both head and tail pointers - Ensure the new tail's 'next' pointer is cleared to prevent memory leaks
- Add logic to disconnect a node from any position in the list - Update head and tail pointers when removing boundary nodes - Nullify removed node pointers to ensure a clean disconnection
- Add constructor with validation to ensure limit is at least 1 - Initialize dictionary for O(1) key-to-node lookups - Initialize LinkedList to track item access order
- Add O(1) lookup functionality using the dictionary - Implement move-to-front logic to mark accessed items as Most Recently Used - Return None for keys not present in the cache
- Add logic to update existing keys and move them to the front (MRU) - Implement eviction of the Least Recently Used (tail) node when the limit is reached - Ensure the lookup dictionary and linked list stay synchronized during eviction - Add new nodes to both the dictionary and the front of the list
- Verify that updating existing keys correctly refreshes their MRU status - Test edge case for a cache with a limit of one - Add test for retrieving non-existent keys (returning None) - Confirm support for complex data types like lists and dictionaries
- Explain hybrid architecture (Dictionary + Doubly Linked List) - Document the "why" behind O(1) time complexity requirements - Detail the space-vs-time trade-off used in the design - Summarize testing strategy for edge cases and eviction logic
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
cjyuan
reviewed
Jul 2, 2026
Comment on lines
+7
to
+11
| def __init__(self, key, value): | ||
| self.key = key | ||
| self.value = value | ||
| self.next = None | ||
| self.previous = None |
There was a problem hiding this comment.
To practice code reuse, you could import the linked list you implemented in the other exercise and store the key-value pairs as a tuple in each node.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Learners, PR Template
Self checklist
Changelist
In this PR, I have implemented a standalone LRU (Least Recently Used) Cache. This task required building a purpose-built Doubly Linked List and Dictionary hybrid to ensure O(1) worst-case time complexity for both
getandsetoperations.A detailed explanation of the architecture and the logic behind the pointer management is available in the
CHANGES_MADE.mdfile.Key Features
NodeandLinkedListclass specifically for the requirements of an LRU Cache, separate from previous tasks.Nodeclass to store both keys and values. This enables the "reverse lookup" required to synchronize the dictionary when a node is evicted from the tail of the list.limitis reached.Testing Done
Verified the implementation with
unittestinlru_cache_test.py, including:getoperations move items to the front of the list.Learning Reflection
This implementation demonstrates the Space-vs-Time trade-off. By using extra memory for a dictionary and a bidirectional linked list, I ensured that the speed of the system is not sacrificed as the data set grows.