Skip to content

Sheffield | 25-SDC-Nov | Sheida Shabankari | Sprint 2 | Implement LRU cache in python#105

Open
sheida-shab wants to merge 6 commits intoCodeYourFuture:mainfrom
sheida-shab:Feat-implement_lru_cache
Open

Sheffield | 25-SDC-Nov | Sheida Shabankari | Sprint 2 | Implement LRU cache in python#105
sheida-shab wants to merge 6 commits intoCodeYourFuture:mainfrom
sheida-shab:Feat-implement_lru_cache

Conversation

@sheida-shab
Copy link

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

PR Summary :
Implement LRU Cache with O(1) get/set operations

Description:
This PR adds a fully functional LRU (Least Recently Used) cache in Python. The implementation includes:

Node class: Represents a key-value pair with pointers to previous and next nodes.

Doubly linked list: Maintains the order of nodes for LRU eviction in O(1) time.

push_head: Moves or adds a node to the head (most recently used).

pop_tail: Removes and returns the tail node (least recently used).

remove: Removes a specific node from the list.

LruCache class: Provides the cache interface.

get(key): Retrieves a value by key and marks the node as most recently used.

set(key, value): Adds or updates a key-value pair; evicts the least recently used node if the cache exceeds its limit.

Handles invalid cache limits by raising ValueError.

Each operation (get and set) has O(1) worst-case time complexity.

Testing:

Unit tests cover cache limit enforcement, eviction order, and get/set functionality.

@sheida-shab sheida-shab added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Complexity The name of the module. labels Feb 16, 2026
Copy link

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Why not simply import or subclass the Double Linked List you implemented in the other exercise (to practice code reuse)?

  • Alternatively, you can explore using OrderedDict within LruCache to maintain order.

Could you improve your LruCache implementation using one of these approaches?

raise ValueError("limit must be greater than zero")
self.limit=limit
self.map={}
self.List=LinkedList()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why start the name the property List with an uppercase L?

def push_head(self,node=None,key=None,value=None) -> Node:
"""
Adds a node to the head of the list.
If 'node' is provided, it is moved to the head.
Copy link

@cjyuan cjyuan Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving a node to the front does not seem like a normal behavior expected from the "push head" operation.

You could consider implementing a helper function or just have the caller explicitly

  1. remove the node to be moved, and
  2. add that node to the front of the list

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Module-Complexity The name of the module. Reviewed Volunteer to add when completing a review with trainee action still to take.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants