Design an LRU Cache with a Constant-Time Average
ConfluentSoftware Engineer · OnsiteLast updated
Design a fixed-capacity LRU cache whose get, put, and current-value average operations all run in expected constant time. Reason through map and recency-list invariants, running-sum updates, replacement and eviction edge cases, numeric precision, overflow, and tests that catch aggregate or ordering bugs.
Design an LRU Cache with a Constant-Time Average
Design an in-memory cache with fixed positive capacity. Keys are unique strings and values are signed numbers. Support:
get(key): return the value and mark the key most recently used, or report a miss.put(key, value): insert or update the key and mark it most recently used; if over capacity, evict the least recently used key.get_average(): return the arithmetic mean of values currently in the cache, or a documented empty result.
All three operations should take expected O(1) time. Explain invariants, numeric choices, and edge cases rather than relying on an ordered-map library as a black box.
Constraints & Assumptions
- Capacity never changes after construction.
- Updating an existing key replaces its value and changes recency.
- The average covers current entries, not historical requests.
- Thread safety is not required for the initial design.
Clarifying Questions to Ask
- Are values integers or floating point, and what precision is expected?
- Does a miss change recency or affect the average?
- What should
get_averagereturn for an empty cache? - Are overflow and concurrent calls in scope?
Follow-up questions
- How would you add TTL expiration without scanning the cache?
- How would a dynamic capacity change work?
- How would you make operations thread-safe and linearizable?
- What changes if values are vectors rather than scalars?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.