ZA | 25-SDC-July | Luke Manyamazi | Sprint 2 | Implement LRU Cache#57
ZA | 25-SDC-July | Luke Manyamazi | Sprint 2 | Implement LRU Cache#57Luke-Manyamazi wants to merge 2 commits intoCodeYourFuture:mainfrom
Conversation
91dd7d7 to
2fd0d3e
Compare
OracPrime
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
Good to see these additional tests. You're the only one I've seen so far that has added these tests.
|
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. |
|
Thank you, David. |
Learners, PR Template
Self checklist
Changelist
Questions