Code RoomCLOCK page cache
HardPrep Room Coding #208

CLOCK page cache

CodingStorage & CDNMid–Senior~35 min

Simulate a page cache using the CLOCK (second-chance) eviction policy. The cache holds at most `capacity` pages arranged in a circular buffer with a clock hand that starts at slot 0. Each page has a reference bit. Process a list of page-id accesses. On access: if the page is resident it is a HIT and its reference bit is set to 1. On a miss, if there is a free slot the page is loaded there (ref bit 1) and the hand is unchanged; if the cache is full, advance the hand, repeatedly clearing ref bits of pages with bit 1 (giving second chances) until a page with bit 0 is found — evict it, load the new page in its slot with ref bit 1, and leave the hand pointing one past that slot. Return the total number of hits. capacity >= 1; <= 10^5 accesses.

Implement
clock_cache_hits(capacity: int, accesses: list[int]) → int
Examples
in[2,[1,2,1,3,2]]out2
What a strong answer looks like

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.

0:00 of about 35 min
InputExpectedGot
[2,[1,2,1,3,2]]2not run yetsample