Reported September 2026
Amazondesign

LRU Cache for Query Results

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The data structure this Amazon OA hinges on is a hash map welded to a doubly linked list. It was reported in September 2026, and it's the classic LRU cache dressed up as "query results." You get a capacity and a list of [1, key] gets and [2, key, value] puts, and you return strings. If you've seen LRU before, this is a 20 minute job. If you haven't, the O(1) requirement will eat you alive. StealthCoder sits invisibly on your screen as a safety net in case you blank mid-assessment, but the pattern below is simple enough to carry in your head.

The problem

Maintain a cache with positive integer capacity. Process each operation atomically in the supplied completed serialization order:
[1, key] performs get(key). Return the stored value, or -1 when the key is absent. A successful get makes the key most recently used.
[2, key, value] performs put(key, value). Insert or update the key and make it most recently used. Updating an existing key does not change the cache size. If an insertion exceeds capacity, evict exactly the least recently used key.
Return one string per operation: the decimal result of each get and the literal string "null" for each put. Implement both operations in O(1) expected time.
The supplied order is a deterministic linearization of completed concurrent operations; an implementation exposed to threads would guard each public operation as one atomic critical section.

Function
runQueryCache(capacity: int, operations: int[][]) → String[]

Examples
Example 1
capacity = 2
operations = [[2,1,10],[2,2,20],[1,1],[2,3,30],[1,2],[1,3]]
return = ["null","null","10","null","-1","30"]
Reading key 1 makes it recent, so inserting key 3 evicts key 2.
Example 2
capacity = 2
operations = [[2,1,5],[2,2,6],[2,1,7],[2,3,8],[1,1],[1,2],[1,3]]
return = ["null","null","null","null","7","-1","8"]
Updating key 1 changes its value and recency without growing the cache, so key 2 is evicted next.

Constraints
1 <= capacity <= 100000.
1 <= operations.length <= 100000.
Every operation has one of the two documented forms.
Keys and values are signed 32-bit integers. The value -1 may be stored and remains distinguishable only by whether the key is present.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two structures working together. A hash map takes key to node in O(1). A doubly linked list orders nodes by recency, with the head as most recent and the tail as least recent. Every get that hits moves the node to the head. Every put either updates and moves the node, or inserts at the head and evicts the tail if size passes capacity. Use sentinel head and tail nodes so you never special-case empty lists. The common pitfalls: forgetting that updating an existing key must not grow size or trigger eviction, forgetting that a get hit refreshes recency, and treating -1 as "missing" when -1 can be a stored value. Check map membership, not the value. Output strings, so a put is the literal "null". If you blank on pointer rewiring live, StealthCoder can hand you the clean implementation as a hedge.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill LRU Cache for Query Results 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as lru cache. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon 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.

LRU Cache for Query Results FAQ

What's the trick in the Amazon LRU Cache OA question?+

Pair a hash map with a doubly linked list. The map gives O(1) lookup of a node by key. The list gives O(1) move-to-front and O(1) removal of the tail. Sentinel head and tail nodes remove nearly all the edge cases around empty or single-node lists.

How hard is this problem really?+

It's a medium if you know LRU and a grind if you don't. The logic is short, but pointer bugs are easy to make under pressure. The extra output format, strings with "null" for puts, is the only twist beyond the standard design problem.

Why does the value -1 matter here?+

Because -1 is both the miss sentinel and a legal stored value. Decide hit or miss by checking whether the key exists in the map, never by comparing the stored value to -1. A stored -1 should still return "-1" and refresh recency.

Do I need to handle threads or locks?+

No. The statement gives you a deterministic order of completed operations, so you just process them sequentially. The concurrency note only says a real threaded version would wrap each operation in one critical section. Ignore it for the code.

How do I prepare for this in 48 hours?+

Write an LRU cache from scratch twice without looking, with a custom doubly linked list, not a built-in ordered map. Then test the update-existing-key case and the capacity 1 case. Those two cases catch most bugs. That's enough for this OA.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it