Byte-Bounded LRU Cache With Per-Entry TTL for Knowledge Lookups

Last updated: November 29, 2026

Quick Overview

Implement a cache for knowledge-base retrieval results that is bounded by total bytes rather than entry count, expires entries after a per-entry TTL, and evicts least recently used entries when over budget.

Sierra
Software Engineering Fundamentals
Software Engineer
Sierra
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Medium

0

0

0 solved


Implement a cache for knowledge-base retrieval results that is bounded by total bytes rather than entry count, expires entries after a per-entry TTL, and evicts least recently used entries when over budget.

Sierra agents repeatedly fetch the same policy articles and order data during conversations, and caching them cuts latency, but stale policy text is a correctness risk. The interviewer checks O(1) LRU mechanics, memory accounting by size, and how expiry interacts with eviction.

What the Interviewer Expects
  • Use an ordered map for O(1) get, put and LRU eviction
  • Track bytes used and evict until under budget
  • Expire entries lazily on read and reclaim expired entries before evicting live ones
  • Reject single items larger than the budget
  • Inject a clock so expiry is testable
Key Topics to Cover
memory and performance
LRU cache
TTL expiry
cache invalidation
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How do you invalidate a cached article the moment a customer edits it?
  • How would you make this safe across threads?
  • When would you choose a shared cache such as Redis over this in-process one?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements

get(key) returns the value or None if missing or expired, and marks it most recently used. put(key, value, size, ttl) inserts or replaces, then evicts until total size is within max_bytes. Ite...

High-Level Design

An OrderedDict gives O(1) move-to-end and pop-oldest. Each entry stores value, size and absolute expiry time. Expiry is lazy: an expired entry is dropped when read. When a put overflows the budget, ...


Submit Your Answer
Markdown supported

Related Questions