Buffer Pool Simulator

medium · database, buffer-pool, cache

Buffer Pool Simulator

Simulate a buffer pool with LRU eviction, pin counts, and dirty tracking.

Types

type Op struct {
    Kind  string // "fetch" or "unpin"
    Page  int
    Dirty bool // only for unpin
}

type Eviction struct {
    Page  int
    Dirty bool
}

func SimulateBufferPool(capacity int, ops []Op) (evictions []Eviction, ok bool)

Rules

  • fetch:
    • If page is already in pool, increment its pin count.
    • Otherwise, if pool has space, load it with pin=1.
    • If full, evict the least recently used unpinned page.
    • If no unpinned page exists, return ok=false.
  • unpin:
    • Decrement pin count if pinned.
    • If Dirty is true, mark the page dirty.

LRU policy

  • Any fetch makes the page the most recently used.
  • Eviction selects the least recently used among unpinned pages.

Output

Return the evicted pages in order (with their dirty flags) and ok=true if all ops succeed.

Run tests to see results
No issues detected