Program for Least Recently Used (LRU) Page Replacement algorithm
Pattern: LRU and LFU cache
Simulate memory with capacity frames, ordered from least to most recently used. A page already in memory is a hit and moves to the most recent end. Otherwise it is a fault: evict the least recently used page if memory is full, then load it.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
Pages are requested in order and memory holds capacity pages. A request for a page not in memory is a page fault:
the page is loaded, first evicting the least recently used page if memory is full. Return the number of faults. The
pages above with capacity 4 give 6.
The catalogue has no practice link for this one.
Think first
- What makes a request a fault?
- On a hit, what changes?
- Which earlier design problem does this simulate?
Hints
Brute force, and why it fails
Keep a last-used time per page and scan for the smallest on each eviction: O(n × capacity).
The key insight
This is LRU Cache with only the counting left. An ordered set or a list plus a map gives O(1) per request.
Dry run
| memory | 7 0 1 2 |
|---|---|
| faults | 4 |
pages 7 0 1 2 0 3 0 4 2 3 0 3 2, capacity 4. 7, 0, 1, 2 are faults: 4 faults.
The plan
for p in pages:
if p in memory: move p to the most recent end; continue
faults += 1
if memory is full: remove the least recent page
add p at the most recent end
Complexity
- Time
O(n)- Space
O(capacity)
Each request does O(1) work with a linked hash set or list plus map.
Edge cases
- Capacity larger than the number of different pages: faults equal the number of different pages.
- Capacity 1: every change of page is a fault.
- Repeated page: a hit.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public int pageFaults(int[] pages, int capacity) {
LinkedHashSet<Integer> memory = new LinkedHashSet<>(); // iteration order = least recently used first
int faults = 0;
for (int p : pages) {
if (memory.remove(p)) { // a hit: re-add it as the most recently used
memory.add(p);
continue;
}
faults++; // a miss: the page must be loaded
if (memory.size() == capacity) {
Iterator<Integer> it = memory.iterator();
it.next();
it.remove(); // evict the least recently used page
}
memory.add(p);
}
return faults;
}
}C++
class Solution {
public:
int pageFaults(vector<int>& pages, int capacity) {
list<int> order; // least recently used at the front
unordered_map<int, list<int>::iterator> where;
int faults = 0;
for (int p : pages) {
auto it = where.find(p);
if (it != where.end()) { // a hit: move it to the back (most recent)
order.erase(it->second);
} else {
faults++; // a miss: the page must be loaded
if (static_cast<int>(order.size()) == capacity) {
where.erase(order.front()); // evict the least recently used page
order.pop_front();
}
}
order.push_back(p);
where[p] = prev(order.end());
}
return faults;
}
};Python
from collections import OrderedDict
class Solution:
def pageFaults(self, pages: list[int], capacity: int) -> int:
memory = OrderedDict() # least recently used first
faults = 0
for p in pages:
if p in memory:
memory.move_to_end(p) # a hit: now the most recently used
continue
faults += 1 # a miss: the page must be loaded
if len(memory) == capacity:
memory.popitem(last=False) # evict the least recently used page
memory[p] = True
return faultsCommon mistakes
- Not updating the order on a hit, which turns it into FIFO replacement.
- Counting a hit as a fault.