FFORGE//RS
← Roadmap

rust / LEVEL 4

The cache whose reads did not count

EST.55 MIN
01

THEORY / RETRIEVAL

What to restore

  • State the recency invariant for reads, updates and insertions
  • Combine a hash index with an index-linked arena
  • Evict and reuse the least-recent slot without stale map entries
  • Separate deliberate key ownership from accidental cloning

A successful read is a state transition

LRU means least recently used, not least recently inserted. A successful get, an update and a new insertion all move that key to the most-recent end. A miss changes nothing. Forgetting the read transition produces a cache that looks correct until a hot old key competes with a newer cold key.

Two structures enforce two independent costs

A HashMap maps each key to a stable slot in average O(1) time. The slots form a doubly linked recency list through Option<usize> handles, so detach, promote and tail eviction touch only neighboring nodes. Scanning a Vec or VecDeque on every hit keeps ordering simple but makes a hot-path get O(n).

Safe indices make the ownership proof reviewable

The map and node arena each own a key, so one explicit String clone is part of this API design. Nodes never move out of their slots; a full cache detaches the tail, removes its old map key, replaces the node payload and reattaches the same slot at the head. Bounds and aliasing remain compiler-checked without raw pointers.

CHECKPOINT

Which operations must move an existing key to the most-recent position? Select every required transition.

Select every applicable option. Credit requires an exact set match.

ISOLATED RUST 1.96
src/lib.rsEDIT

02 / IMPLEMENTATION

Implement the contract

Complete LruCache with average O(1) get and put. Successful get, update and insertion become most recent; misses do not change order. A full insertion returns the evicted key/value, updates never evict, and len never exceeds capacity. Keep the public API, use no unsafe code and do not linearly scan entries.

Initializing editor…
CLOUD SANDBOXnetwork off · 256 MB · 12 s
2 / 64 KB
OUTPUT
Runner is waiting for a submission.