Priority, Expiration, and LRU Eviction
Reported by candidates from Tesla's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Tesla OA from March 2021 dresses up as a cache design problem, but it reduces to a single pass over four arrays with a tuple comparison. No data structure to build. If you're taking it in the next day or two, the trap is overthinking it. You're picking one key to evict at time now, using a strict precedence: expired first, then lowest priority, then oldest lastUsed, then smallest key. StealthCoder is there as a safety net if you blank on the live OA, but this one is very doable on your own.
The problem
A cache currently contains n items. Item i has key keys[i], priority priorities[i], expiration time expirations[i], and last-used time lastUsed[i]. Select the key of the one item to evict at time now using this precedence: If at least one item is expired, only expired items are eligible. An item is expired when expirations[i] <= now. Return the smallest eligible key. Otherwise, keep only items with the smallest priority value. If several items have that priority, choose the least recently used one: the item with the smallest lastUsed value. If a tie still remains, return the smallest key. Function selectEvictionKey(keys: int[], priorities: int[], expirations: int[], lastUsed: int[], now: int) → int Examples Example 1 keys = [10,20,30] priorities = [5,1,1] expirations = [100,40,40] lastUsed = [9,8,7] now = 50 return = 20 Keys 20 and 30 are expired. Expiration takes precedence over priority and recency, and the smaller eligible key is 20. Example 2 keys = [1,2,3,4] priorities = [2,1,1,3] expirations = [100,100,100,100] lastUsed = [5,8,3,1] now = 50 return = 3 No item is expired. Keys 2 and 3 have the minimum priority, and key 3 has the earlier last-used time. Example 3 keys = [9,4] priorities = [1,1] expirations = [100,100] lastUsed = [7,7] now = 50 return = 4 The items tie on expiration status, priority, and recency, so the smaller key is returned. Constraints 1 <= n <= 10^5. All four arrays have length n. All keys are distinct. 0 <= keys[i], priorities[i], expirations[i], lastUsed[i], now <= 10^9. Smaller lastUsed values represent less recent access.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that the rules are a lexicographic ordering with one twist. Expiration isn't a sort key, it's a filter. So first scan for any item where expirations[i] <= now. If at least one exists, restrict to those. Then pick the minimum by the tuple (priority, lastUsed, key) over the eligible set. For expired items, the spec says return the smallest key, so don't apply priority there. That's the common pitfall: Example 1 has key 30 with lower priority tie but key 20 wins by key only. Keep it O(n) with a single scan, no sorting needed, though sorting works at 10^5. Watch the <= on expiration, since equal to now counts as expired. If you freeze on the live OA, StealthCoder can hand you the filter-then-min structure so you just verify against the three examples.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Priority, Expiration, and LRU Eviction cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Tesla's OA.
Tesla reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Priority, Expiration, and LRU Eviction FAQ
How hard is the Tesla priority, expiration and LRU eviction problem really?+
Easy to medium. There's no clever algorithm. It's careful reading of the precedence rules. Most failures come from applying priority to expired items or using < instead of <= for expiration. One linear pass solves it.
What's the trick to selectEvictionKey?+
Treat expiration as a filter, not a sort key. If any item is expired, return the smallest expired key. Otherwise take the minimum by (priority, lastUsed, key). Two scans or one scan with a comparison function both work.
Do I need a heap or an actual LRU linked list?+
No. You only evict once per call, so a heap or doubly linked list is wasted effort. A single O(n) scan with tuple comparison handles n up to 10^5 easily. Building a full cache design would just add bugs.
Which edge cases should I test before submitting?+
Test an item with expirations[i] equal to now, which counts as expired. Test all items expired with different priorities. Test ties on priority and lastUsed so the smallest key decides. Test n = 1. Run the three given examples too.
How do I prepare for this in 48 hours?+
Practice writing min-by-tuple selection and filter-then-reduce loops in your language. Re-read the spec twice and write the precedence as comments before coding. Work the three examples by hand. That's more useful than studying real cache designs.