Caching interview problems
Caching questions range from writing an LRU cache class to making an async cache that doesn't stampede. You should know functools.lru_cache and functools.cache (and that they don't work on coroutines in a useful way, since they cache the coroutine object, which can be awaited only once). You should also know OrderedDict.move_to_end for LRU order and storing (value, expires_at) for TTL. For async caches, the key idea is storing an in-flight Future or Task per key so concurrent misses share one load.
The classic mistakes are TTL checks that compare against time.time(), eviction that forgets to remove the key from every structure, and caching exceptions forever when a load fails. In async code the big one is the thundering herd. Ten coroutines miss at once and all call the backend. A strong answer states the eviction policy and the capacity unit, decides whether a failed load is cached, uses a monotonic clock, and explains how the cache behaves under concurrency. Expect follow-ups on invalidation and on stale-while-revalidate.
1 open problem
- 21Async TTL Cache with Request Coalescingmedium
Build an asyncio cache whose entries expire after a TTL, where concurrent misses for the same key share one fetch that survives caller cancellation.
2 locked
These open in order of demand. Open one and press I want this one to ask for it.
- 46Disk-Backed LRU Artifact Cache Lockedmedium
Build a byte-budgeted LRU cache that stores model artifacts as files on disk, evicting least recently used entries and keeping only metadata in memory.
- 49Reference-Counted Model Manager Lockedhard
Load each model once for many concurrent users, unload it when the last user releases it, and handle load failures and acquires during unload.