coding · Cursor · free
Implement an LRU cache in [LANGUAGE] with production grade concerns, not a coding interview answer. Requirements: Capacity: [N] entries OR [M] bytes (which do I need?) Thread safe? [Y/N — if Y, describe concurrency level] TTL support: [Y/N — sliding vs absolute] Value type: [FIXED / GENERIC] Metrics needed: hits, misses, evictions, size Deliver: 1. The core structure — hashmap + doubly linked list (or the language idiomatic equivalent) 2. get(key) — O(1), promotes to head, respects TTL 3. set(key, value, ttl?) — O(1), evicts LRU on capacity, handles duplicate keys 4. delete(key) — O(1) 5. Concurrency: pick ONE — coarse mutex, striped locks, or lock free — justify for MY read/write ratio 6. Eviction hook so callers can react (close file handles, log) 7. A stats method returning the metrics 8. Tests covering: capacity eviction, TTL expiry, promotion on read, concurrent access if enabled Anti patterns to reject: JSON serialization for cache keys, unbounded weak refs pretending to be LRU, silent swallowing of type mismatches.
#data-structures #caching #implementation