Least Recently Used Cache
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that sinks a naive LRU is an update to an existing key. Goldman Sachs reported this Least Recently Used Cache OA in September 2026, and it's the classic design problem wrapped in a string-parsing function. You get operations like "PUT 5 9" and "GET 5", and you return only the GET results. If you've got an OA invite, this is the one to nail cleanly. Hash map plus doubly linked list, O(1) per operation. StealthCoder is the safety net if your mind goes blank on the pointer wiring during the live assessment.
The problem
Process a sequence of operations on a least recently used cache with capacity capacity. PUT key value inserts or updates a key. An update makes the key most recently used. If a new insertion exceeds capacity, evict the least recently used key. GET key returns the value, or -1 when absent. A hit makes the key most recently used; a miss does not change recency. Return all GET results in operation order. Operation fields are decimal integers separated by one space. Function runLRU(capacity: int, operations: String[]) → int[] Examples Example 1 capacity = 2 operations = ["PUT 1 10","PUT 2 20","GET 1","PUT 3 30","GET 2","GET 3"] return = [10,-1,30] The lookup of key 1 makes key 2 least recently used, so inserting key 3 evicts key 2. Example 2 capacity = 1 operations = ["PUT 5 7","PUT 5 9","GET 5","PUT 6 4","GET 5"] return = [9,-1] Updating key 5 replaces its value without adding a second entry. Inserting key 6 later evicts it. Example 3 capacity = 3 operations = ["GET 8","PUT 8 0","GET 8"] return = [-1,0] The first lookup misses. After insertion, a stored value of 0 is returned normally. Constraints 1 <= capacity <= 10^5 1 <= operations.length <= 2 * 10^5 Every operation is exactly GET key or PUT key value. -10^9 <= key, value <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is pairing a hash map (key to node) with a doubly linked list ordered by recency. GET on a hit moves the node to the front. PUT on an existing key updates the value and moves it to the front, with no eviction and no second entry. PUT on a new key inserts at the front, then evicts the tail if size exceeds capacity. The pitfalls are in the examples. A miss must not touch recency. A stored value of 0 is valid, so don't test truthiness, check key presence. Keys and values can be negative, so parse them as integers. Use sentinel head and tail nodes to avoid null checks. In a language with an ordered map, that works too. If the pointer logic slips under pressure, StealthCoder can supply a working version live. Parse each string by splitting on a space.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Least Recently Used 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
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 Goldman Sachs's OA.
Goldman Sachs reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Least Recently Used Cache FAQ
How hard is the Goldman Sachs LRU cache question really?+
It's medium difficulty but very well known. The logic is simple once you know the hash map plus doubly linked list structure. The difficulty is clean implementation under time pressure, especially pointer updates. If you've written it once, it's roughly a 20 minute job.
What's the trick to get O(1) for both GET and PUT?+
Use a hash map from key to list node, and a doubly linked list ordered by recency. The map gives O(1) lookup. The list gives O(1) move-to-front and O(1) removal of the tail. Sentinel head and tail nodes remove the edge-case branches.
What edge cases does this problem test?+
Updating an existing key must not grow the cache or trigger eviction. A GET miss must not change recency. A value of 0 must be returned, not treated as missing. Capacity 1 is allowed, so eviction happens on every new key. Negative keys and values must parse correctly.
Can I use a built-in ordered map instead of writing a linked list?+
Usually yes, if the language allows it. Python's OrderedDict or Java's LinkedHashMap with access order does the job in a few lines. Know the manual version anyway, since the assessment may restrict library use or you may need to explain it.
How do I prepare in 48 hours for this OA?+
Write the hash map plus doubly linked list LRU from scratch twice, without looking. Then run the three examples by hand, including the capacity 1 update case. Practice parsing the operation strings into a command and integers. Test large input sizes mentally for O(1) per operation.