• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
May 07, 2024 |160 Views
SDE Sheet - Page Faults in LRU
  Share   Like
Description
Discussion

This video is part of the Greedy section under GFG SDE Sheet.

In this problem, we are given a sequence of pages in an array pages[] of length N and memory capacity C, find the number of page faults using Least Recently Used (LRU) Algorithm.

Note:- Before solving this example revising the OS LRU cache mechanism is recommended.

Example :

Input: N = 9, C = 4
pages = {5, 0, 1, 3, 2, 4, 1, 0, 5}
Output: 8
Explaination: memory allocated with 4 pages 5, 0, 1, 3: page fault = 4
page number 2 is required, replaces LRU 5: 
page fault = 4+1 = 5
page number 4 is required, replaces LRU 0: 
page fault = 5 + 1 = 6
page number 1 is required which is already present: 
page fault = 6 + 0 = 6
page number 0 is required which replaces LRU 3: 
page fault = 6 + 1 = 7
page number 5 is required which replaces LRU 2: 
page fault = 7 + 1  = 8.

Try it out before watching the implementation of the problem in the video. We recommend watching the video, even if you can solve the problem. You may discover something new. All the best!!!

Do check out:-
Article: https://www.geeksforgeeks.org/page-faults-in-lru-implementation/
Problem: https://www.geeksforgeeks.org/problems/page-faults-in-lru5603/1
SDE Sheet Link: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/