coding
Tesla
Google
Amazon

Tesla Coding Interview: Priority Expiry Cache Eviction

Topics:
Priority Queue
Heap
LRU Cache
Roles:
Software Engineer
Backend Engineer
Experience:
Entry Level
Mid Level
Senior

Question Description

You are asked to implement evictItem() for a cache that must evict exactly one entry according to a strict precedence: expired items first, then the item with the lowest numeric priority, and if multiple items share that lowest priority, the least-recently-used (LRU) item among them.

This question treats set/get as blackbox operations that already store items with fields (key, value, priority, expireTime) and update recency metadata. Your evictItem() should locate a single candidate, remove it from the cache storage, and return its key (or None/null if the cache is empty). You must not change how set/get work or how recency is updated.

Flow you’ll be asked to explain or implement:

  • Check for any expired items (expireTime < now) and evict one if present.
  • If none expired, find the minimum priority value and evict one item with that priority.
  • If multiple items tie on priority, pick the LRU among them using the maintained recency metadata.

Skill signals interviewers expect:

  • Familiarity with priority queues/heaps and min-heap ordering
  • Use of timestamps and lazy-deletion to keep auxiliary heaps in sync with main storage
  • Handling of edge cases (empty cache, concurrent modifications, equal expireTime or priority)
  • Time/space complexity reasoning (aim for O(log n) per eviction with lazy cleanup)

Practical tips: describe data structures you’d maintain (e.g., an expire-time min-heap and a priority min-heap keyed by (priority, lastAccessTime)), explain lazy invalidation when set/get mutates storage, and outline unit tests and common pitfalls (clock drift, identical timestamps).

Common Follow-up Questions

  • How would you design the auxiliary data structures (heaps or trees) to keep evictItem() O(log n) while set/get remain black boxes?
  • How do you handle lazy deletion or stale heap entries when set() updates an item's expireTime or priority?
  • If many items are expired, how would you modify evictItem() to efficiently evict multiple entries or batch-clean the cache?
  • How would you change the eviction precedence if priority could change dynamically after insertion (and how to keep it efficient)?

Related Questions

1Implement an LRU cache with O(1) get and set operations
2Design a cache with time-based expiry and automatic cleanup
3Implement a priority queue that supports deletion and update-key operations
4Design a hybrid eviction policy combining LFU and priority

Explore More Questions

Practice This Question with AI

Get real-time hints, detailed requirements, and insightful analysis of the question.

Priority Expiry Cache Eviction - Tesla Coding Interview | Voker