Bird
Raised Fist0
Interview Prepoperating-systemsmediumAmazonGoogleMicrosoftRazorpayPhonePe

Page Replacement - FIFO, LRU, Optimal Algorithm

Choose your preparation mode3 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
🎯
Page Replacement - FIFO, LRU, Optimal Algorithm
mediumOSAmazonGoogleMicrosoft

Imagine your computer's memory as a small desk where you can keep only a few books open at once. When you need a new book but the desk is full, you must decide which book to close to make space. This decision is what page replacement algorithms solve in operating systems.

💡 Beginners often confuse page replacement algorithms with simple caching or think all algorithms behave similarly under all conditions, missing nuances like Belady's anomaly or the difference between practical and theoretical optimality. Think of page replacement as managing a limited workspace where you must decide which items to remove to make room for new ones, balancing simplicity and efficiency.
📋
Interview Question

Explain the concept of page replacement in operating systems and compare the FIFO, LRU, and Optimal page replacement algorithms. What are their working principles, advantages, disadvantages, and typical use cases?

Page fault and its impact on system performanceHow FIFO, LRU, and Optimal algorithms decide which page to evictTrade-offs between simplicity, accuracy, and implementation complexity
💡
Scenario & Trace
ScenarioA system with limited physical memory frames receives a sequence of page requests from a running process.
When a requested page is not in memory (page fault), the OS must select a page to evict. FIFO evicts the oldest loaded page regardless of usage; LRU evicts the page that was least recently used, approximating future usage; Optimal evicts the page that will not be used for the longest time in the future, which is ideal but impractical to implement.
  • When all pages in memory are equally old (FIFO) → FIFO evicts the oldest without considering usage
  • When multiple pages have the same least recent use time (LRU) → tie-breaking strategies may vary
  • Belady's anomaly in FIFO → increasing frames can sometimes increase page faults
  • When future page references are unknown (Optimal) → Optimal algorithm is theoretical and cannot be implemented exactly
⚠️
Common Mistakes
Confusing Optimal algorithm as practically implementable

Interviewer doubts your understanding of theoretical vs practical algorithms

Clarify that Optimal is a theoretical benchmark requiring future knowledge, not implementable in real systems

Assuming FIFO always improves with more frames

Interviewer suspects lack of knowledge about Belady's anomaly

Explain that FIFO can suffer from Belady's anomaly where more frames can increase page faults

Thinking LRU is free of overhead

Interviewer questions your grasp of implementation complexity

Mention that LRU requires tracking usage history, which can be costly in time or hardware

Mixing page replacement with page fault handling mechanisms

Interviewer finds your explanation unfocused or inaccurate

Separate the concept of page fault detection from the replacement strategy used after a fault

🧠
Basic Definition - What It Is
💡 This level covers the fundamental idea of page replacement and why it is needed in operating systems. Think of it as understanding the problem before diving into solutions.

Intuition

Page replacement algorithms decide which memory page to remove when new pages need to be loaded into limited physical memory.

Explanation

In operating systems, physical memory is limited and divided into fixed-size frames. When a process requests a page not currently in memory, a page fault occurs. To handle this, the OS must replace an existing page with the new one. Page replacement algorithms provide strategies to select which page to evict. FIFO removes the oldest loaded page, LRU removes the least recently used page, and Optimal removes the page that will not be needed for the longest time in the future.

Memory Hook

💡 Think of a revolving door where the first person in is the first person out (FIFO), versus a library where you remove the book you haven't read in the longest time (LRU), versus a crystal ball that tells you which book you won't need for the longest time (Optimal).

Interview Questions

What is the main goal of a page replacement algorithm?
  • To decide which page to evict on a page fault
  • To minimize page faults and improve system performance
Depth Level
Interview Time30 seconds
Depthbasic

Covers the core concept and basic differences between FIFO, LRU, and Optimal.

Interview Target: Minimum floor - never go below this

Knowing only this will help you pass initial screening but is insufficient for deeper technical rounds.

🧠
Mechanism Depth - How It Works
💡 This level explains the internal workings, trade-offs, and practical considerations of each algorithm. Understanding these details helps you discuss implementation and performance in interviews.

Intuition

Each algorithm uses a different heuristic to approximate the best page to evict, balancing complexity and performance.

Explanation

FIFO maintains a queue of pages in the order they were loaded; when a page fault occurs, it evicts the page at the front of the queue. This is simple but can lead to suboptimal decisions, including Belady's anomaly where more frames cause more faults. LRU tracks page usage over time, evicting the page that has not been used for the longest period, approximating the optimal future knowledge. Implementing LRU requires additional data structures or hardware support, such as counters or stacks. The Optimal algorithm, while not implementable in practice, serves as a benchmark by evicting the page that will not be used for the longest time in the future, minimizing page faults theoretically. Understanding these mechanisms helps in analyzing trade-offs between implementation complexity and fault rates.

Memory Hook

💡 Imagine FIFO as a queue line, LRU as a 'last touched' list, and Optimal as a perfect oracle predicting future needs.

Illustrative Code

def fifo_page_replacement(pages, capacity):
    queue = []  # Queue to store pages in FIFO order
    page_set = set()  # Set for O(1) lookup
    page_faults = 0
    for page in pages:
        if page not in page_set:
            page_faults += 1
            if len(queue) == capacity:
                removed = queue.pop(0)  # Remove oldest page
                page_set.remove(removed)
            queue.append(page)
            page_set.add(page)
    return page_faults

def lru_page_replacement(pages, capacity):
    page_faults = 0
    page_map = {}  # Map page to its last used index
    pages_in_memory = set()
    for i, page in enumerate(pages):
        if page not in pages_in_memory:
            page_faults += 1
            if len(pages_in_memory) == capacity:
                # Evict least recently used page
                lru_page = min(page_map, key=page_map.get)
                pages_in_memory.remove(lru_page)
                del page_map[lru_page]
            pages_in_memory.add(page)
        page_map[page] = i  # Update last used index
    return page_faults

def optimal_page_replacement(pages, capacity):
    page_faults = 0
    pages_in_memory = set()
    for i in range(len(pages)):
        page = pages[i]
        if page not in pages_in_memory:
            page_faults += 1
            if len(pages_in_memory) == capacity:
                # Find page with farthest future use
                farthest = -1
                page_to_remove = None
                for p in pages_in_memory:
                    try:
                        next_use = pages[i+1:].index(p)
                    except ValueError:
                        next_use = float('inf')
                    if next_use > farthest:
                        farthest = next_use
                        page_to_remove = p
                pages_in_memory.remove(page_to_remove)
            pages_in_memory.add(page)
    return page_faults

Interview Questions

    Depth Level
    Interview Time
    Depth

    FIFO uses a queue and set for O(1) operations per page, resulting in O(n) time. LRU requires searching for least recently used page, which can be O(capacity) per page, leading to O(n * capacity). Optimal requires scanning future pages to find farthest use, also O(n * capacity). Space is proportional to capacity for all.

    Interview Target: Target level for FAANG on-sites

    Mastering this level distinguishes you from most candidates and prepares you for system design discussions.

    📊
    Explanation Depth Levels
    💡 Choose your explanation depth based on interview stage and company expectations.
    LevelInterview TimeSuitable ForRisk
    Basic Definition30sScreening call or initial roundsToo shallow for on-site or system design interviews
    Mechanism Depth2-3 minutesOn-site interviews at FAANG and top tech companiesRequires good understanding of trade-offs and implementation details
    💼
    Interview Strategy
    💡 Use this guide to structure your explanation clearly and confidently before every interview. Analogies and clear stepwise explanations help interviewers follow your thought process.

    How to Present

    Start with a clear definition of page replacement and why it is necessary.Give a simple analogy or example to illustrate the concept.Explain the working principles of FIFO, LRU, and Optimal algorithms.Discuss edge cases like Belady's anomaly and practical limitations.Conclude with trade-offs and typical use cases.

    Time Allocation

    Definition: 30s → Example: 1min → Mechanism: 2min → Edge cases: 30s. Total ~4min

    What the Interviewer Tests

    Your ability to explain concepts clearly, understand trade-offs, and handle edge cases distinguishes pass from fail.

    Common Follow-ups

    • What is Belady's anomaly and why does it occur?
    • How can LRU be implemented efficiently in hardware or software?
    💡 These follow-ups test deeper understanding and practical knowledge beyond definitions.
    🔍
    Pattern Recognition

    When to Use

    Interviewers ask about page replacement when discussing memory management, virtual memory, or system performance optimization.

    Signature Phrases

    'Explain page replacement algorithms''Compare FIFO, LRU, and Optimal''What happens when a page fault occurs?'

    NOT This Pattern When

    Similar Problems

    Practice

    (1/5)
    1. In which scenario is Round Robin scheduling with a fixed time quantum most appropriate compared to First-Come-First-Serve or Priority scheduling?
    easy
    A. When processes have highly variable CPU burst times and fairness among all processes is required
    B. When all processes have identical priorities and long CPU bursts
    C. When minimizing average turnaround time is the only goal
    D. When processes have strict deadlines and real-time constraints

    Solution

    1. Step 1: Understand Round Robin's fairness goal

      Round Robin scheduling is designed to allocate CPU time slices fairly among all processes, especially when burst times vary widely.
    2. Step 2: Analyze other options

      When all processes have identical priorities and long CPU bursts is less suitable because identical priorities and long bursts favor FCFS or SJF for efficiency, not fairness. When minimizing average turnaround time is the only goal ignores fairness and focuses only on turnaround time, which RR does not optimize. When processes have strict deadlines and real-time constraints requires real-time guarantees, which RR cannot provide due to its time slicing.
    3. Final Answer:

      Option A -> Option A
    4. Quick Check:

      RR is best when fairness and responsiveness matter, especially with variable burst times.
    Hint: RR = fairness with variable bursts
    Common Mistakes:
    • Assuming RR always minimizes turnaround time
    • Believing RR suits real-time deadlines
    • Confusing RR with priority-based scheduling
    2. Trace the sequence of events when a process exceeds its working set size causing thrashing. What happens step-by-step inside the system?
    easy
    A. The process generates page faults, the OS increases its frame allocation, and thrashing stops immediately
    B. The process generates page faults, the OS swaps out pages not in the working set, but thrashing continues due to insufficient frames
    C. The process generates page faults, the OS ignores them, and the process continues without delay
    D. The process generates page faults, the OS reduces the number of processes to free frames, preventing thrashing

    Solution

    1. Step 1: Identify what happens when working set is exceeded

      The process causes frequent page faults because its active pages exceed allocated frames.
    2. Step 2: OS response

      The OS attempts to swap out pages not in the working set to free frames, but if frames are insufficient, thrashing persists.
    3. Step 3: Analyze options

      A is incorrect because OS cannot always increase frames immediately; thrashing may continue.
      B is correct as it describes the OS swapping out pages but thrashing continuing due to frame shortage.
      C is incorrect because OS does not ignore page faults.
      D is incorrect because load control (reducing processes) is a prevention technique but not an immediate response to page faults.
    4. Final Answer:

      Option B -> Option B
    5. Quick Check:

      Thrashing occurs when working set exceeds frames; OS tries to swap but may fail if frames are insufficient.
    Hint: Thrashing = page faults + insufficient frames despite swapping
    Common Mistakes:
    • Assuming OS can instantly allocate more frames
    • Believing OS ignores page faults
    • Confusing immediate load control with page fault handling
    3. Which of the following statements about the 'Mutual Exclusion' condition in deadlock is INCORRECT?
    medium
    A. Mutual exclusion means that at least one resource must be held in a non-shareable mode.
    B. Mutual exclusion can be eliminated for all resources to prevent deadlock.
    C. Mutual exclusion is necessary for deadlock but not sufficient alone.
    D. Mutual exclusion applies only to resources that cannot be simultaneously used by multiple processes.

    Solution

    1. Step 1: Understand mutual exclusion

      It requires that some resources be non-shareable, meaning only one process can use them at a time.
    2. Step 2: Analyze why eliminating mutual exclusion for all resources is impractical

      Eliminating mutual exclusion for all resources is impossible because some resources (like printers) inherently cannot be shared.
    3. Step 3: Evaluate other options

      Mutual exclusion means that at least one resource must be held in a non-shareable mode correctly defines mutual exclusion. Mutual exclusion is necessary for deadlock but not sufficient alone correctly states it is necessary but not sufficient. Mutual exclusion applies only to resources that cannot be simultaneously used by multiple processes correctly limits mutual exclusion to non-shareable resources.
    4. Final Answer:

      Option B -> Option B
    5. Quick Check:

      Mutual exclusion cannot be eliminated for all resources; some must be exclusive.
    Hint: Mutual exclusion is about non-shareable resources, which can't all be made shareable [OK]
    Common Mistakes:
    • Thinking mutual exclusion can be removed entirely
    • Confusing necessity with sufficiency
    • Misapplying mutual exclusion to shareable resources
    4. Which of the following statements about deadlock prevention in the Dining Philosophers problem is INCORRECT?
    medium
    A. Using asymmetric fork picking (odd philosophers pick left first, even pick right first) guarantees no starvation
    B. Allowing a philosopher to pick up both forks only if both are available prevents deadlock
    C. Imposing a strict ordering on resource acquisition always prevents deadlock
    D. Breaking one of the four Coffman conditions is sufficient to prevent deadlock

    Solution

    1. Step 1: Analyze each statement

      Imposing a strict ordering on resource acquisition always prevents deadlock is correct: ordering resources prevents circular wait; Allowing a philosopher to pick up both forks only if both are available prevents deadlock is correct: atomic acquisition avoids partial hold; Breaking one of the four Coffman conditions is sufficient to prevent deadlock is correct: breaking any Coffman condition prevents deadlock.
    2. Step 2: Evaluate Using asymmetric fork picking (odd philosophers pick left first, even pick right first) guarantees no starvation

      Asymmetric fork picking prevents deadlock but does not guarantee no starvation, as some philosophers may be repeatedly delayed.
    3. Final Answer:

      Option A -> Option A
    4. Quick Check:

      Only Using asymmetric fork picking (odd philosophers pick left first, even pick right first) guarantees no starvation incorrectly claims starvation is guaranteed prevented.
    Hint: Deadlock prevention ≠ starvation prevention [OK]
    Common Mistakes:
    • Assuming deadlock prevention implies starvation freedom
    • Believing asymmetric picking solves all synchronization issues
    • Confusing Coffman conditions with starvation conditions
    5. In a system using priority scheduling with aging to prevent starvation, what unexpected behavior might occur if a process holding a critical resource is preempted by an aged higher-priority process, and how can this be mitigated?
    hard
    A. Livelock will occur due to continuous priority changes; mitigation involves fixed priorities.
    B. Deadlock will occur immediately; mitigation requires disabling aging.
    C. Priority inversion may occur, delaying the critical resource release; mitigation involves priority inheritance protocols.
    D. Starvation will worsen for low-priority processes; mitigation involves removing aging.

    Solution

    1. Step 1: Identify the problem

      When a low-priority process holding a critical resource is preempted by a higher-priority process (due to aging), priority inversion can occur because the resource holder cannot proceed to release the resource.
    2. Step 2: Understand mitigation

      Priority inheritance protocols temporarily raise the priority of the resource-holding process to prevent inversion and reduce blocking time.
    3. Step 3: Analyze options

      Priority inversion may occur, delaying the critical resource release; mitigation involves priority inheritance protocols correctly identifies priority inversion and its mitigation. Deadlock will occur immediately; mitigation requires disabling aging incorrectly claims deadlock occurs immediately and suggests disabling aging, which is not a solution. Livelock will occur due to continuous priority changes; mitigation involves fixed priorities confuses livelock with priority changes. Starvation will worsen for low-priority processes; mitigation involves removing aging incorrectly states starvation worsens and suggests removing aging, which would increase starvation risk.
    4. Final Answer:

      Option C -> Option C
    5. Quick Check:

      Priority inversion + priority inheritance = correct diagnosis and fix.
    Hint: Priority inversion = resource holder preempted; fix with inheritance
    Common Mistakes:
    • Confusing priority inversion with deadlock
    • Thinking aging always prevents all scheduling issues