LRU Cache Snapshot Printer
Reported by candidates from Hebbia's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Hebbia OA reported in February 2026 looks like a plain LRU cache, and that's the trap. You'll write the usual hash map plus recency order in your sleep, then the SNAPSHOT operation shows up and quietly breaks the lazy version. It's a design problem with a formatting twist: group keys by value, keep groups in first-appearance order, and print most recent to least recent. If you're taking this in the next day or two, know the snapshot rules cold. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the pattern below is simple enough to own before you start.
The problem
Process operations on a fixed-capacity LRU cache. PUT key value inserts or updates an entry and contributes null. GET key contributes its value, or the empty string when absent, and makes a hit most recently used. Evict the least recently used entry after an overflowing PUT. SNAPSHOT contributes a compressed representation from most to least recent. Scan entries in that order, group keys with equal values, keep value groups in first-appearance order, and format each group as value:[key1,key2], joined with semicolons. Function runCacheSnapshots(capacity: int, operations: String[][]) → String[] Examples Example 1 capacity = 2 operations = [["PUT","A","2"],["PUT","B","2"],["SNAPSHOT"],["GET","A"],["SNAPSHOT"]] return = ["null","null","2:[B,A]","2","2:[A,B]"] Snapshot order follows recency, and a GET moves A to the front. Example 2 capacity = 2 operations = [["PUT","A","x"],["PUT","B","y"],["PUT","C","x"],["GET","A"],["SNAPSHOT"]] return = ["null","null","null","","x:[C];y:[B]"] A is evicted; groups retain the first value appearance in recency order. Constraints 1 <= capacity <= 1000 1 <= operations.length <= 100000 Keys and values contain no comma, colon, brackets, or semicolon.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Core structure: a hash map plus a doubly linked list, or an ordered map, so GET and PUT stay O(1). Move an entry to the front on a hit and on any PUT of an existing key. Evict from the tail only when an insert pushes size past capacity. The pitfalls are in SNAPSHOT. Walk entries from most to least recent, and use a map from value to a list of keys, plus a separate list recording the order each value first appeared. Don't sort the groups. Don't merge keys by insertion time. Example 2 shows it: C and A share x, but A is evicted, so x:[C] leads. A GET miss returns the empty string and must not touch recency. A PUT that updates a value also refreshes recency. With up to 100000 operations and capacity at most 1000, a snapshot costs O(capacity), which is fine. If the live OA freezes you, StealthCoder can hand you the skeleton fast.
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 LRU Cache Snapshot Printer 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
You've seen the question.
Make sure you actually pass Hebbia's OA.
Hebbia 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 Snapshot Printer FAQ
How hard is the Hebbia LRU Cache Snapshot Printer really?+
Medium. The LRU part is a classic you've likely seen. The snapshot formatting is where people lose points, because the grouping and ordering rules are easy to misread. Code the cache first, then handle SNAPSHOT as a separate pass over entries in recency order.
What's the trick for grouping values in SNAPSHOT?+
Iterate entries from most to least recent. Use a map from value to a list of keys, and record each value the first time you see it in an order list. Then output groups in that order as value:[k1,k2], joined by semicolons. No sorting needed.
Does a GET miss or a PUT update change recency?+
A GET miss returns the empty string and changes nothing. A GET hit moves the key to most recent. A PUT on an existing key updates the value and also makes it most recent. A PUT on a new key inserts at the front, then evicts the tail if over capacity.
What should the output array contain for each operation?+
One string per operation. PUT gives the string null, GET gives the value or an empty string on a miss, and SNAPSHOT gives the formatted string. Example 1 shows this: the first two outputs are null, then 2:[B,A], then 2, then 2:[A,B].
How do I prepare for this in 48 hours?+
Write an LRU cache from scratch twice, using a linked list with a map, until it's automatic. Then add a snapshot function and test it against both examples. Also try edge cases like capacity 1, repeated PUTs on one key, and all values equal.