What the interviewer just asked
You paused after the prompt. Now gather requirements — write them on the board so the interviewer can course-correct early.
Functional questions to ask
- What operations? Usually
get(key)andput(key, value). - What is the capacity? Fixed at construction time, or configurable later?
- What happens on eviction? Remove the least-recently-used entry when inserting would exceed capacity.
- What does
getreturn on a miss? Typically-1(orOptional.empty()if they prefer). - Does
puton an existing key update recency? Yes — treat it like a fresh access; move that entry to most-recently-used. - Do we need
remove(key)orsize()? Ask; many variants include them, the classic LeetCode prompt does not.
Non-functional questions
- Thread-safe? Almost always yes in an LLD concurrency round.
- Single JVM or distributed? Start in-memory, single process unless they insist on multi-node.
- Exact LRU or approximate? Strict LRU reorders on every
get. Approximate LRU (batch recency updates) trades correctness for throughput — name the trade-off if they care about QPS. - Expected throughput / read-write ratio? Heavy read traffic does not automatically mean
ReadWriteLockhelps —getmutates the list in strict LRU (covered later).
What you produce on the board
Functional
get(key)— return value if present; return-1on miss; update recency on hitput(key, value)— insert or update; evict LRU entry when size would exceed capacity- Fixed capacity set at construction
Non-functional
- Thread-safe for concurrent
getandput - In-memory, single JVM for v1
- O(1)
getandput - Exact LRU semantics unless interviewer accepts approximate recency
Out of scope (say explicitly)
- Distributed cache across pods (Redis, Hazelcast — discuss later)
- Persistence across restarts
- TTL-based expiry (that is a different cache design)
- Cache statistics, metrics, or admin UI
Scope sentence you can say out loud
“I’ll build a thread-safe in-memory LRU cache with O(1) get and put, fixed capacity, LRU eviction, and -1 on miss. I’ll start with the data structure, show LinkedHashMap as the idiomatic Java answer, then walk through synchronized locking and why ReadWriteLock does not help for strict LRU.”