Skip to content
Tactiverge

□ Computer systems · simulation with arrays

Count cache hits

Simulate a small least-recently-used cache and count hits and misses for a stream of requests.

  • Level 4 · Systems Engineer
  • 5 tests
  • 🐞 debug variant
  • technically checked

Your task

A cache keeps a few recently used items close at hand. When it is full and a new item arrives, a least-recently-used (LRU) cache evicts the item that has gone unused for the longest.

Write lruHits(requests, capacity) returning { hits, misses }:

  • a request already in the cache is a hit, and that item becomes the most recently used;
  • otherwise it is a miss: if the cache is full, evict the least recently used item, then add the new one as most recently used;
  • a capacity of 0 means every request misses.

Hints (0 of 3 shown)

    What gets checked

    • lruHits([1, 2, 1, 3, 2, 4, 1], 2) → { hits: 1, misses: 6 }
    • lruHits([1, 1, 1], 1) → { hits: 2, misses: 1 }

    Plus 3 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.

    Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments

    Starting the sandbox…

    Press Run to see what your program prints.

    Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.