Cache with expiry, priority and LRU eviction
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Tesla's technical phone screen, last asked Aug 2025. It tests design, cache and heap.
Implement a fixed-capacity key-value cache, PriorityExpiryLRUCache. Every item stores a value, an integer priority, and an expiry time. When a new key arrives and the cache is full, exactly one item is evicted, following these rules in order:
- If any item has expired (
expiry <= now), evict an expired item. Choose the one with the earliest expiry. - Otherwise, evict an item with the lowest priority.
- If several items share that lowest priority, evict the least recently used one. Both
getandsetcount as a use.
API
class PriorityExpiryLRUCache:
def __init__(self, capacity: int) -> None: ...
def get(self, key: str, now: int) -> Any | None: ...
def set(self, key: str, value: Any, priority: int, expiry: int, now: int) -> None: ...
-
nowis the current time, passed in so behavior is deterministic. -
getreturns the value and marks the key as recently used. It returnsNonefor a missing key. It also returnsNonefor an expired key, and removes that key. -
seton an existing key replaces its value, priority and expiry. It never causes an eviction. -
seton a new key evicts one item first if the cache is at capacity.
Constraints
-
1 <= capacity <= 10^5; up to10^6calls in total. - Aim for
getin O(1) andsetin O(log n), not an O(n) scan per eviction.
Example 1
Capacity 2.
set("cpu", 1, priority=4, expiry=30, now=0)
set("gpu", 2, priority=8, expiry=5, now=0)
set("ram", 3, priority=1, expiry=30, now=10) # gpu expired at 5: evict gpu
# cache: {cpu, ram}
gpu had the higher priority but had expired, so it goes first.
Example 2
Capacity 3. Nothing expires before time 100.
set("a", "A", priority=2, expiry=100, now=0)
set("b", "B", priority=2, expiry=100, now=1)
set("c", "C", priority=6, expiry=100, now=2)
get("a", now=3) # returns "A"; b is now the LRU at priority 2
set("d", "D", priority=5, expiry=100, now=4) # evict b
# cache: {a, c, d}
Clarifying questions
- Is an item with
expiry == nowalready expired (the rule saysexpiry <= now)? - When several items are expired, is it always the earliest expiry, and how are ties on expiry broken?
- Does
seton an existing key count as a use for LRU, and can it change the key's priority? - Is a larger number a higher priority, so priority
1goes before priority4?
What a strong answer covers
- A hash map from key to entry, plus one ordered dict (or linked list) per priority for LRU order, so
getmoves an entry to the back inO(1). - A min-heap of priorities that currently have entries, and a min-heap of
(expiry, key)for expired eviction; both use lazy deletion with a version check, because updates andgetremovals leave stale heap entries. - Eviction order: pop stale tops, evict the earliest expired item if its expiry is
<= now, else the front of the lowest priority's LRU list. -
getinO(1)andsetinO(log n)amortized, with stale entries bounded by the number of calls. - Tests both examples, updating an existing key's priority and expiry, capacity 1, and every item expired at once.
Follow-ups
- Make it safe for concurrent callers.
- Remove expired items in the background instead of only on access.
- Add
delete(key)and resizing the capacity.
Where it's asked
- Tesla · Technical phone screen · Last asked Aug 202545 min · 10 to 20 minutes on your background, then one compact coding, debugging or design task in CoderPad that must run
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
