← Questions

Cache with expiry, priority and LRU eviction

0
TeslaCodingLast asked Aug 2025

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:

  1. If any item has expired (expiry <= now), evict an expired item. Choose the one with the earliest expiry.
  2. Otherwise, evict an item with the lowest priority.
  3. If several items share that lowest priority, evict the least recently used one. Both get and set count 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: ...
  • now is the current time, passed in so behavior is deterministic.
  • get returns the value and marks the key as recently used. It returns None for a missing key. It also returns None for an expired key, and removes that key.
  • set on an existing key replaces its value, priority and expiry. It never causes an eviction.
  • set on a new key evicts one item first if the cache is at capacity.

Constraints

  • 1 <= capacity <= 10^5; up to 10^6 calls in total.
  • Aim for get in O(1) and set in 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 == now already expired (the rule says expiry <= now)?
  • When several items are expired, is it always the earliest expiry, and how are ties on expiry broken?
  • Does set on 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 1 goes before priority 4?

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 get moves an entry to the back in O(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 and get removals 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.
  • get in O(1) and set in O(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

design · cache · heap · hash map

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding