From 27e2d22116413cd7cbd935a0b758b6cfe3cbb7d7 Mon Sep 17 00:00:00 2001 From: Odio Marcelino Date: Sun, 29 Jun 2025 19:38:22 +0600 Subject: [PATCH] Fix AttributeError in LRU cache eviction (missing `query` on [Node](cci:2://file:///c:/Users/T2430514/Downloads/system-design-primer/solutions/object_oriented_design/lru_cache/lru_cache.py:0:0-4:24)) The sample LRU cache crashes on eviction because [Node](cci:2://file:///c:/Users/T2430514/Downloads/system-design-primer/solutions/object_oriented_design/lru_cache/lru_cache.py:0:0-4:24) instances lack a `query` attribute, yet `Cache.set()` references `tail.query` when removing the oldest item. Added `query` (and minor docs) to [Node](cci:2://file:///c:/Users/T2430514/Downloads/system-design-primer/solutions/object_oriented_design/lru_cache/lru_cache.py:0:0-4:24) and create nodes with both key and value, eliminating the runtime error and restoring cache functionality. Co-Authored-By: S. M. Mohiuddin Khan Shiam <147746955+mohiuddin-khan-shiam@users.noreply.github.com> --- .../object_oriented_design/lru_cache/lru_cache.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/solutions/object_oriented_design/lru_cache/lru_cache.py b/solutions/object_oriented_design/lru_cache/lru_cache.py index acee46516eb..723859986c8 100644 --- a/solutions/object_oriented_design/lru_cache/lru_cache.py +++ b/solutions/object_oriented_design/lru_cache/lru_cache.py @@ -1,8 +1,17 @@ class Node(object): - def __init__(self, results): + def __init__(self, query, results): + """Doubly-linked list node used by the LRU cache. + + Args: + query: The cache key this node represents. + results: Cached value for *query*. + """ + self.query = query self.results = results - self.next = next + # Pointers for the doubly-linked list + self.prev = None + self.next = None class LinkedList(object): @@ -61,6 +70,6 @@ def set(self, results, query): else: self.size += 1 # Add the new key and value - new_node = Node(results) + new_node = Node(query, results) self.linked_list.append_to_front(new_node) self.lookup[query] = new_node