LRU Cache
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Salesforce reported this one in September 2026, and it's the classic LRU Cache wrapped in a batch function. The real task is a hash map plus a recency ordering, both updated in O(1). If your OA invite lands in the next few days, expect this shape: operations array, arguments array, collect the get results. StealthCoder sits behind the live assessment as a safety net if the linked list pointers fall out of your head mid-timer. The rest of this page gives you the pattern first, so you probably won't need it.
The problem
Process operations on a least recently used cache with fixed positive capacity. The cache starts empty. put has arguments [key, value]. Insert or update the key and make it most recently used. If a new key makes the cache exceed capacity, evict the least recently used key. A put produces no result. get has arguments [key]. Return its value and make the key most recently used, or return -1 when the key is absent. Return the results of all get operations in operation order. Function runLruCache(capacity: int, operations: String[], arguments: int[][]) → int[] Examples Example 1 capacity = 2 operations = ["put","put","get","put","get","get"] arguments = [[1,1],[2,2],[1],[3,3],[2],[3]] return = [1,-1,3] Reading key 1 makes it recent. Inserting key 3 then evicts key 2. Example 2 capacity = 1 operations = ["put","put","get","put","get"] arguments = [[5,10],[5,20],[5],[6,30],[5]] return = [20,-1] Updating key 5 changes its value without eviction. Inserting key 6 later evicts key 5. Example 3 capacity = 2 operations = ["get","put","get","get"] arguments = [[7],[7,70],[7],[7]] return = [-1,70,70] The first read misses. After insertion, repeated successful reads return 70 and preserve recency. Constraints 1 <= capacity <= 10^5 1 <= operations.length = arguments.length <= 2 * 10^5 Each operation is exactly get or put. A get row has one integer; a put row has two integers. 0 <= key, value <= 10^9 Every operation must run in average O(1) time.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is pairing a hash map with a doubly linked list. The map takes a key to its list node, so lookup is O(1). The list holds recency order, with the most recent at one end and the least recent at the other. On get, move the node to the front. On put, update and move if the key exists, otherwise insert at the front and evict the tail when size passes capacity. Use dummy head and tail nodes so you never branch on null. The common pitfalls are forgetting that put on an existing key also refreshes recency, forgetting to delete the evicted key from the map, and skipping the -1 on a miss. Here you only collect results from get calls, so don't append anything for put. In a JS or Python OA, an ordered dictionary shortcut works, but the constraint says average O(1), so know the manual version. If you freeze, StealthCoder can feed you the node-splice code live.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill LRU Cache 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as lru cache. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Salesforce's OA.
Salesforce reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
LRU Cache FAQ
What's the trick to the Salesforce LRU Cache problem?+
Hash map for key to node, doubly linked list for recency order. Every get and put moves a node to the front in O(1). Evict from the tail when size exceeds capacity. Dummy head and tail nodes remove most of the edge cases.
How hard is this OA question really?+
Medium on paper, but it's a memorized pattern. If you've written it once, it takes about 15 minutes. If you haven't, pointer bugs in the linked list will eat your time. The logic is simple. The bookkeeping is where people slip.
Can I use a built-in ordered map instead of writing a linked list?+
Often yes. Python's OrderedDict and JS Map keep insertion order, and you can delete and reinsert a key to refresh it. That gives O(1) average time. Still know the manual version in case the language lacks one or the assessment restricts it.
What edge cases break most solutions?+
Updating an existing key without refreshing recency, evicting without removing the key from the map, capacity 1, and get on a missing key before any put. Example 2 and Example 3 in the problem cover several of these, so trace them by hand.
How do I prepare for this in 48 hours?+
Write the doubly linked list version from scratch twice, with no notes. Then run the three given examples by hand. Focus on the helper functions: remove node, add to front, and move to front. Once those are clean, get and put are short.