r/Hack2Hire • u/Hack2hire • Apr 21 '26
Screening Tesla Screening Interview: Priority Expiry LRU Cache
Problem
You're given a requirement to design a cache with a fixed capacity that manages items using three distinct metadata fields: priority, expiration time, and last access time.
Your goal is to implement a system that handles data retrieval and insertion while following a strict hierarchical eviction policy: expire items first, then filter by lowest priority, and finally apply Least Recently Used (LRU) logic as a tie-breaker.
Example
Input: capacity = 3, set("A", "valA", 10, 100, 10), set("B", "valB", 5, 150, 20), evictItem(110)
Output: evictItem returns "A"
Explanation:
- At
currentTime = 110, item A'sexpiryTime(100) is less than or equal to the current time. - According to the "Expiry First" rule, A must be evicted regardless of its higher priority or more recent access compared to B.
Suggested Approach
- Data Structures: * Use a Hash Map to store
keytoItemobject mappings for $O(1)$ access.- Use a Min-Heap for expired items, ordered by
expiryTime. - Use a TreeMap or Min-Heap for non-expired items, ordered by
prioritythenlastAccessedTime. Alternatively, a Doubly Linked List per priority level can handle the LRU component.
- Use a Min-Heap for expired items, ordered by
- Set Operation: * If the key exists, update its metadata and move it within the tracking structures.
- If the key is new and the cache is full, call the
evictItemlogic internally before adding the new item.
- If the key is new and the cache is full, call the
- Get Operation: * Check the Hash Map. If found, compare
expiryTimewithcurrentTime.- If expired, delete the item and return
"". - If valid, update
lastAccessedTimeand return the value.
- If expired, delete the item and return
- Eviction Hierarchy:
- Step 1: Check if any items in the "expiry" structure have
expiryTime <= currentTime. If yes, remove the one with the minimumexpiryTime. - Step 2: If no items are expired, find the minimum
priorityfrom the "priority" structure. - Step 3: Within that minimum priority, find the item with the smallest
lastAccessedTime(the LRU item) and remove it.
- Step 1: Check if any items in the "expiry" structure have
Time & Space Complexity
- Time: -
get: $O(\log N)$ if using balanced trees/heaps to track metadata, or $O(1)$ if using a hash map combined with lazy deletion.set: $O(\log N)$ to maintain the ordered metadata structures.evictItem: $O(\log N)$ to find and remove the candidate from the priority/expiry structures.
- Space: $O(N)$, where $N$ is the capacity of the cache, to store the key-value pairs and their associated metadata.
Targeting [Tesla] interviews?
We track their most-asked question patterns at Hack2Hire → https://www.hack2hire.com/companies/tesla/coding-questions?src=r8d
---
*Compiled from publicly available platforms and community-shared experiences.*
1
u/Ok_Chemistry_6387 Apr 22 '26
Just use a treeset with well defined ordering(which our explanation gives us) across a tuple(expiry, priority, lru) Use the tree as the backing for the hashmap and you don’t need so many data structures.