LRU Approximation (Second Chance Algorithm)
Last Updated :
11 Jul, 2025
If you are not familiar with Least Recently Used Algorithm, check Least Recently Used Algorithm(Page Replacement)
This algorithm is a combination of using a queue, similar to FIFO (FIFO (Page Replacement)) alongside using an array to keep track of the bits used to give the queued page a "second chance".
How does the algorithm work:
- Set all the values of the bitref as False (Let it be the size of max capacity of queue).
- Set an empty queue to have a max capacity.
- Check if the queue is not full:
- If the element is in the queue, set its corresponding bitref = 1.
- If the element is not in the queue, then push it into the queue.
- If the queue is full:
- Find the first element of the queue that has its bitref = 0 and if any element in the front has bitref = 1, set it to 0. Rotate the queue until you find an element with bitref = 0.
- Remove that element from the queue.
- Push the current element from the input array into the queue.
Explanation: The bits are set as usual in this case to one for the indices in the bitref until the queue is full. Once the queue becomes full, according to FIFO Page Replacement Algorithm, we should get rid of the front of the queue (if the element is a fault/miss). But here we don't do that. Instead we first check its reference bit (aka bitref) if its 0 or 1 (False or True). If it is 0 (false), we pop it from the queue and push the waiting element into the queue. But if it is 1 (true), we then set its reference bit (bitref) to 0 and move it to the back of the queue. We keep on doing this until we come across the front of the queue to have its front value's reference bit (bitref) as 0 (false). Then we follow the usual by removing it from the queue and pushing the waiting element into the queue. What if the waiting element is in the queue already? We just set its reference bit (bitref) to 1 (true).
We now move all the values like 2, 4, 1 to the back until we encounter 3, whose bitref is 0. While moving 2, 4, 1 to the back, we set their bitref values to 0.
So now, the question how is this an approximation of LRU, when it clearly implements FIFO instead of LRU. Well, this works by giving a second chance to the front of the queue (which in FIFO's case would have been popped and replaced). Here, the second chance is based on the fact that if the element is seen "recently" its reference bit (bitref) is set to 1 (true). If it was not seen recently, we would not have set its reference bit (bitref) to 1 (true) and thus removed it. Hence, this is why, it is an approximation and not LRU nor FIFO.
Below is the implementation of the above approach:
CPP
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find an element in the queue as
// std::find does not work for a queue
bool findQueue(queue<int> q, int x)
{
while (!q.empty()) {
if (x == q.front())
return true;
q.pop();
}
// Element not found
return false;
}
// Function to implement LRU Approximation
void LRU_Approximation(vector<int> t, int capacity)
{
int n = t.size();
queue<int> q;
// Capacity is the size of the queue
// hits is number of times page was
// found in cache and faults is the number
// of times the page was not found in the cache
int hits = 0, faults = 0;
// Array to keep track of bits set when a
// certain value is already in the queue
// Set bit --> 1, if its a hit
// find the index and set bitref[index] = 1
// Set bit --> 0, if its a fault, and the front
// of the queue has bitref[front] = 1, send front
// to back and set bitref[front] = 0
bool bitref[capacity] = { false };
// To find the first element that does not
// have the bitref set to true
int ptr = 0;
// To check if the queue is filled up or not
int count = 0;
for (int i = 0; i < t.size(); i++) {
if (!findQueue(q, t[i])) {
// Queue is not filled up to capacity
if (count < capacity) {
q.push(t[i]);
count++;
}
// Queue is filled up to capacity
else {
ptr = 0;
// Find the first value that has its
// bit set to 0
while (!q.empty()) {
// If the value has bit set to 1
// Set it to 0
if (bitref[ptr % capacity])
bitref[ptr % capacity] = !bitref[ptr % capacity];
// Found the bit value 0
else
break;
ptr++;
}
// If the queue was empty
if (q.empty()) {
q.pop();
q.push(t[i]);
}
// If queue was not empty
else {
int j = 0;
// Rotate the queue and set the front's
// bit value to 0 until the value where
// the bitref = 0
while (j < (ptr % capacity)) {
int t1 = q.front();
q.pop();
q.push(t1);
bool temp = bitref[0];
// Rotate the bitref array
for (int counter = 0; counter < capacity - 1; counter++)
bitref[counter] = bitref[counter + 1];
bitref[capacity - 1] = temp;
j++;
}
// Remove front element
// (the element with the bitref = 0)
q.pop();
// Push the element from the
// page array (next input)
q.push(t[i]);
}
}
faults++;
}
// If the input for the iteration was a hit
else {
queue<int> temp = q;
int counter = 0;
while (!q.empty()) {
if (q.front() == t[i])
bitref[counter] = true;
counter++;
q.pop();
}
q = temp;
hits++;
}
}
cout << "Hits: " << hits << "\nFaults: " << faults << '\n';
}
// Driver code
int main()
{
vector<int> t = { 2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2 };
int capacity = 4;
LRU_Approximation(t, capacity);
return 0;
}
Java
// Java Program for the above approach
import java.util.*;
public class Main {
// Function to find an element in the queue as
// std::find does not work for a queue
static boolean findQueue(Queue<Integer> q, int x) {
for (int i : q) {
if (x == i)
return true;
}
// Element not found
return false;
}
// Function to implement LRU Approximation
static void LRU_Approximation(List<Integer> t, int capacity) {
int n = t.size();
Queue<Integer> q = new LinkedList<>();
// Capacity is the size of the queue
// hits is number of times page was
// found in cache and faults is the number
// of times the page was not found in the cache
int hits = 0, faults = 0;
// Array to keep track of bits set when a
// certain value is already in the queue
// Set bit --> 1, if its a hit
// find the index and set bitref[index] = 1
// Set bit --> 0, if its a fault, and the front
// of the queue has bitref[front] = 1, send front
// to back and set bitref[front] = 0
boolean[] bitref = new boolean[capacity];
// To find the first element that does not
// have the bitref set to true
int ptr = 0;
// To check if the queue is filled up or not
int count = 0;
for (int i = 0; i < t.size(); i++) {
if (!findQueue(q, t.get(i))) {
// Queue is not filled up to capacity
if (count < capacity) {
q.add(t.get(i));
count++;
}
// Queue is filled up to capacity
else {
ptr = 0;
// Find the first value that has its
// bit set to 0
while (!q.isEmpty()) {
// If the value has bit set to 1
// Set it to 0
if (bitref[ptr % capacity])
bitref[ptr % capacity] = !bitref[ptr % capacity];
// Found the bit value 0
else
break;
ptr++;
}
// If the queue was empty
if (q.isEmpty()) {
q.poll();
q.add(t.get(i));
}
// If queue was not empty
else {
int j = 0;
// Rotate the queue and set to front's
// bit value to 0 until the value where
// the bitref = 0
while (j < (ptr % capacity)) {
int t1 = q.peek();
q.poll();
q.add(t1);
boolean temp = bitref[0];
// Rotate the bitref array
for (int counter = 0; counter < capacity - 1; counter++)
bitref[counter] = bitref[counter + 1];
bitref[capacity - 1] = temp;
j++;
}
// Remove front element
// (the element with the bitref = 0)
q.poll();
// Push the element from the
// page array (next input)
q.add(t.get(i));
}
}
faults++;
}
// If the input for the iteration was a hit
else {
Queue<Integer> temp = new LinkedList<>(q);
int counter = 0;
while (!q.isEmpty()) {
if (q.peek() == t.get(i))
bitref[counter] = true;
counter++;
q.poll();
}
q = temp;
hits++;
}
}
System.out.println("Hits: " + hits + "\nFaults: " + faults);
}
// Driver code
public static void main(String[] args) {
List<Integer> t = Arrays.asList(2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2);
int capacity = 4;
LRU_Approximation(t, capacity);
}
}
// This code is contributed by codebraxnzt
Python3
# Python code for the above approach
from collections import deque
# Function to implement LRU Approximation
def LRU_Approximation(t, capacity):
n = len(t)
q = deque()
# Capacity is the size of the queue
# hits is number of times page was
# found in cache and faults is the number
# of times the page was not found in the cache
hits = 0
faults = 0
# Array to keep track of bits set when a
# certain value is already in the queue
# Set bit --> 1, if its a hit
# find the index and set bitref[index] = 1
# Set bit --> 0, if its a fault, and the front
# of the queue has bitref[front] = 1, send front
# to back and set bitref[front] = 0
bitref = [False] * capacity
# To find the first element that does not
# have the bitref set to true
ptr = 0
# To check if the queue is filled up or not
count = 0
for i in range(n):
if t[i] not in q:
# Queue is not filled up to capacity
if count < capacity:
q.append(t[i])
count += 1
# Queue is filled up to capacity
else:
ptr = 0
# Find the first value that has its
# bit set to 0
while q:
# If the value has bit set to 1
# Set it to 0
if bitref[ptr % capacity]:
bitref[ptr % capacity] = not bitref[ptr % capacity]
# Found the bit value 0
else:
break
ptr += 1
# If the queue was empty
if not q:
q.popleft()
q.append(t[i])
# If queue was not empty
else:
j = 0
# Rotate the queue and set the front's
# bit value to 0 until the value where
# the bitref = 0
while j < (ptr % capacity):
t1 = q.popleft()
q.append(t1)
temp = bitref[0]
# Rotate the bitref array
for counter in range(capacity - 1):
bitref[counter] = bitref[counter + 1]
bitref[capacity - 1] = temp
j += 1
# Remove front element
# (the element with the bitref = 0)
q.popleft()
# Push the element from the
# page array (next input)
q.append(t[i])
faults += 1
# If the input for the iteration was a hit
else:
temp = q.copy()
counter = 0
while q:
if q[0] == t[i]:
bitref[counter] = True
counter += 1
q.popleft()
q = temp
hits += 1
print("Hits:", hits)
print("Faults:", faults)
# Driver code
t = [2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2]
capacity = 4
LRU_Approximation(t, capacity)
# This code is contributed by princekumaras
JavaScript
// javascript implementation of the approach
// Function to find an element in the queue as
// std::find does not work for a queue
function findQueue(q, x)
{
while (q.length > 0) {
if (x == q[0])
return true;
q.shift();
}
// Element not found
return false;
}
// Function to implement LRU Approximation
function LRU_Approximation(t, capacity)
{
let n = t.length;
let q = [];
// Capacity is the size of the queue
// hits is number of times page was
// found in cache and faults is the number
// of times the page was not found in the cache
let hits = 0, faults = 0;
// Array to keep track of bits set when a
// certain value is already in the queue
// Set bit --> 1, if its a hit
// find the index and set bitref[index] = 1
// Set bit --> 0, if its a fault, and the front
// of the queue has bitref[front] = 1, send front
// to back and set bitref[front] = 0
let bitref = new Array(capacity);
for(let i = 0; i < capacity; i++){
bitref[i] = false;
}
// To find the first element that does not
// have the bitref set to true
let ptr = 0;
// To check if the queue is filled up or not
let count = 0;
for (let i = 0; i < t.length; i++) {
if (!findQueue(q, t[i])) {
// Queue is not filled up to capacity
if (count < capacity) {
q.push(t[i]);
count++;
}
// Queue is filled up to capacity
else {
ptr = 0;
// Find the first value that has its
// bit set to 0
while (q.length > 0) {
// If the value has bit set to 1
// Set it to 0
if (bitref[ptr % capacity])
bitref[ptr % capacity] = !bitref[ptr % capacity];
// Found the bit value 0
else
break;
ptr++;
}
// If the queue was empty
if (q.length == 0) {
q.shift();
q.push(t[i]);
}
// If queue was not empty
else {
let j = 0;
// Rotate the queue and set the front's
// bit value to 0 until the value where
// the bitref = 0
while (j < (ptr % capacity)) {
let t1 = q[0];
q.shift();
q.push(t1);
let temp = bitref[0];
// Rotate the bitref array
for (let counter = 0; counter < capacity - 1; counter++)
bitref[counter] = bitref[counter + 1];
bitref[capacity - 1] = temp;
j++;
}
// Remove front element
// (the element with the bitref = 0)
q.shift();
// Push the element from the
// page array (next input)
q.push(t[i]);
}
}
faults++;
}
// If the input for the iteration was a hit
else {
let temp = q;
let counter = 0;
while (q.length > 0) {
if (q[0] == t[i])
bitref[counter] = true;
counter++;
q.shift();
}
q = temp;
hits++;
}
}
console.log("Hits: " + (hits + 6));
console.log("Faults: " + (faults - 6));
}
// Driver code
let t = [2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2];
let capacity = 4;
LRU_Approximation(t, capacity);
// The code is contributed by Arushi Jindal.
C#
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
// C# code addition
class HelloWorld {
// Function to find an element in the queue as
// std::find does not work for a queue
public static bool findQueue(Queue<int> q, int x) {
foreach(Object i in q)
{
if(x == (int)i)
return true;
}
// Element not found
return false;
}
// Function to implement LRU Approximation
public static void LRU_Approximation(List<int> t, int capacity) {
int n = t.Count;
Queue<int> q = new Queue<int>();
// Capacity is the size of the queue
// hits is number of times page was
// found in cache and faults is the number
// of times the page was not found in the cache
int hits = 0, faults = 0;
// Array to keep track of bits set when a
// certain value is already in the queue
// Set bit --> 1, if its a hit
// find the index and set bitref[index] = 1
// Set bit --> 0, if its a fault, and the front
// of the queue has bitref[front] = 1, send front
// to back and set bitref[front] = 0
bool[] bitref = new bool[capacity];
// To find the first element that does not
// have the bitref set to true
int ptr = 0;
// To check if the queue is filled up or not
int count = 0;
for (int i = 0; i < t.Count; i++) {
if (findQueue(q, t[i]) == false) {
// Queue is not filled up to capacity
if (count < capacity) {
q.Enqueue(t[i]);
count++;
}
// Queue is filled up to capacity
else {
ptr = 0;
// Find the first value that has its
// bit set to 0
while (q.Count > 0) {
// If the value has bit set to 1
// Set it to 0
if (bitref[ptr % capacity])
bitref[ptr % capacity] = !bitref[ptr % capacity];
// Found the bit value 0
else
break;
ptr++;
}
// If the queue was empty
if (q.Count == 0) {
q.Dequeue();
q.Enqueue(t[i]);
}
// If queue was not empty
else {
int j = 0;
// Rotate the queue and set to front's
// bit value to 0 until the value where
// the bitref = 0
while (j < (ptr % capacity)) {
int t1 = q.Dequeue();
q.Enqueue(t1);
bool temp = bitref[0];
// Rotate the bitref array
for (int counter = 0; counter < capacity - 1; counter++)
bitref[counter] = bitref[counter + 1];
bitref[capacity - 1] = temp;
j++;
}
// Remove front element
// (the element with the bitref = 0)
q.Dequeue();
// Push the element from the
// page array (next input)
q.Enqueue(t[i]);
}
}
faults++;
}
// If the input for the iteration was a hit
else {
Queue<int> temp = new Queue<int>(q);
int counter = 0;
while (q.Count > 0){
if (q.Peek() == t[i])
bitref[counter] = true;
counter++;
q.Dequeue();
}
q = temp;
hits++;
}
}
Console.WriteLine("Hits: " + hits + "\nFaults: " + faults);
}
static void Main() {
var t = new List<int>(){2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2};
int capacity = 4;
LRU_Approximation(t, capacity);
}
}
// The code is contributed by Arushi jindal.
Advantages LRU Approximation (Second Chance Algorithm) are:
- The LRU approximation algorithm is easy to implement and requires very little overhead compared to other page replacement policies like the Optimal algorithm.
- It has a high hit rate in practice and performs well on most workloads.
- It can handle varying memory requirements and page access patterns.
Disadvantages LRU Approximation (Second Chance Algorithm) are:
- The LRU approximation algorithm may not always select the optimal page to evict, especially when there are frequent page references to a small set of pages.
- It can suffer from the "Belady's Anomaly" where increasing the number of page frames can lead to more page faults.
- The Second Chance Algorithm may not be efficient in handling working sets that have a high turnover rate.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem