Skip to content

ZA | 25-SDC-July | Luke Manyamazi | Sprint 2 | Implement LRU Cache#57

Open
Luke-Manyamazi wants to merge 2 commits intoCodeYourFuture:mainfrom
Luke-Manyamazi:implement_lru_cache
Open

ZA | 25-SDC-July | Luke Manyamazi | Sprint 2 | Implement LRU Cache#57
Luke-Manyamazi wants to merge 2 commits intoCodeYourFuture:mainfrom
Luke-Manyamazi:implement_lru_cache

Conversation

@Luke-Manyamazi
Copy link

@Luke-Manyamazi Luke-Manyamazi commented Nov 10, 2025

Learners, PR Template

Self checklist

  • 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

Changelist

  • Implemented get and put methods with O(1) time complexity
  • Used hash map + doubly linked list for efficient access tracking
  • Handles cache eviction in put when capacity is exceeded

Questions

  1. Is the current LRU eviction policy in put correct?
  2. Should we add utility methods like size() or clear()?

@Luke-Manyamazi Luke-Manyamazi 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 Nov 10, 2025
Copy link

@OracPrime OracPrime left a comment

Choose a reason for hiding this comment

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

The code works, but it doesn't meet the O(1) requirement, so needs a little revision.

def get(self, key):
if key in self.cache:
# Move to most recent position
self.access_order.remove(key)

Choose a reason for hiding this comment

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

Although this code works, list.remove has to scan through the list to find key. This isn't O(1), which is the requirement of this test. What O( ) is it?. How else might you manage the order without this?

self.assertEqual(cache.get("a"), 1) # "a" remains
self.assertEqual(cache.get("c"), 3)

def test_complex_usage_pattern(self):

Choose a reason for hiding this comment

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

Good to see these additional tests. You're the only one I've seen so far that has added these tests.

Copy link
Author

Choose a reason for hiding this comment

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

Oooh thank you, David.

@OracPrime OracPrime 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 Jan 26, 2026
@Luke-Manyamazi
Copy link
Author

I did some research on achieving O(1) LRU behaviour and found that using OrderedDict is a standard and efficient approach. I replaced the list-based access tracking (O(n) removal) with OrderedDict, which provides constant-time updates and eviction while preserving correct LRU behaviour and meeting the required performance constraints.

@Luke-Manyamazi Luke-Manyamazi added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. 📅 Sprint 2 Assigned during Sprint 2 of this module labels Feb 13, 2026
Copy link

@OracPrime OracPrime left a comment

Choose a reason for hiding this comment

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

This will work!

@OracPrime OracPrime added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Feb 13, 2026
@Luke-Manyamazi
Copy link
Author

Thank you, David.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. Module-Complexity The name of the module. Reviewed Volunteer to add when completing a review with trainee action still to take. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants