Buffer pool eviction
A database keeps hot pages in a buffer pool of capacity slots and replaces by least recently used. The list accesses holds the page ids the engine touches, in order. Touching a page that is already resident makes it the most recently used one. Touching a page that is not resident loads it into a free slot, and when no slot is free the least recently used unpinned resident page is evicted to make room. The list pinned holds the page ids an operator is holding open. A pinned page can be touched and can become most recently used, but it is never evicted. When the pool is full and every resident page is pinned, the incoming page is read straight from disk and the pool is left exactly as it was. Return the resident page ids, most recently used first.
buffer_pool_residents(accesses: list[str], capacity: int, pinned: list[str]) → list[str][["p1","p2","p3","p1","p4"],3,[]]out["p4","p1","p3"][["p1","p2","p3","p4"],3,["p1"]]out["p4","p3","p1"][["p9","p9","p9"],2,[]]out["p9"]State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
[["p1","p2","p3","p1","p4"],3,[]]["p4","p1","p3"]not run yetsample[["p1","p2","p3","p4"],3,["p1"]]["p4","p3","p1"]not run yetsample[["p9","p9","p9"],2,[]]["p9"]not run yetsample