LFU Cache
Reported by candidates from Arista Networks's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Arista Networks reported this one in September 2026, and the whole question hinges on one data structure choice: how you track frequency buckets with recency order inside each bucket. It's an LFU Cache, framed as a command-processing function that returns only the get results. Every operation has to run in average O(1), so a sorted structure or a scan for the minimum won't pass. If you've seen LRU, this is its harder cousin. You can build it cleanly in about 40 lines once you see the layout. If you blank during the live OA, StealthCoder runs invisibly on your desktop as a safety net and reads the problem for you.
The problem
Process commands on an initially empty least-frequently-used cache with positive integer capacity. put key value inserts a new key or updates an existing key. get key returns the stored value, or -1 when the key is absent. For this exercise, assume a successful get and a put that updates an existing key each increase that key's frequency by one and make it most recently used within its new frequency. A new key starts with frequency one. When a new insertion would exceed capacity, evict the key with the smallest frequency. Break a frequency tie by evicting the least recently used key. Return the results of all get commands in order. Each operation should run in average O(1) time. Function processLfuCache(capacity: int, operations: String[]) → int[] Examples Example 1 capacity = 2 operations = ["put 1 1","put 2 2","get 1","put 3 3","get 2","get 3","put 4 4","get 1","get 3","get 4"] return = [1,-1,3,-1,3,4] Reading key 1 raises its frequency, so key 2 is evicted first. Later keys 1 and 3 tie on frequency, and key 1 is least recent. Example 2 capacity = 1 operations = ["put 7 5","put 7 8","get 7","put 9 4","get 7","get 9"] return = [8,-1,4] Updating key 7 changes its value and frequency. Inserting key 9 into a one-entry cache then evicts key 7. Constraints 1 <= capacity <= 3000. 1 <= operations.length <= 2 * 10^4. Each command is exactly get key or put key value. Keys and values fit in signed 32-bit integers. At least one command is get.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is three maps working together. One map holds key to value and frequency. A second maps each frequency to an ordered set of keys, insertion-ordered so the oldest sits first. A third is just a single integer, minFreq. On get or update, remove the key from its current bucket, bump its frequency, append it to the next bucket, and if the old bucket was empty and equaled minFreq, increment minFreq. On a new insert at capacity, evict the first key in the minFreq bucket, then set minFreq to 1. The classic pitfall is forgetting that a put on an existing key also raises frequency, and that this problem says so explicitly. Another is failing to reset minFreq after inserting a new key. Capacity is at least 1, so you won't hit the zero-capacity edge case. If the bucket logic slips under pressure, StealthCoder is the hedge for the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill LFU 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as lfu cache. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Arista Networks's OA.
Arista Networks reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
LFU Cache FAQ
What's the trick to LFU Cache in O(1)?+
Keep a frequency to ordered-keys map plus a minFreq counter. Each access moves a key from bucket f to bucket f+1. Eviction pops the oldest key from the minFreq bucket. An insertion-ordered set or doubly linked list gives you O(1) removal and append.
How hard is the Arista Networks LFU Cache really?+
It's a hard-tier design problem, but the logic is mechanical once you know the layout. The difficulty is bookkeeping, not insight. Most failures come from forgetting to update minFreq or mishandling the update-existing-key case.
Does put on an existing key change frequency here?+
Yes. This version states that a put updating an existing key increases its frequency by one and makes it most recently used within the new frequency. It also overwrites the value. Miss this and your eviction order in Example 2 breaks.
How do I handle the tie-break when frequencies match?+
Evict the least recently used key within the minimum frequency bucket. If each bucket is an insertion-ordered structure, the first element is always the oldest. Moving a key to a new bucket appends it at the end, which marks it most recent.
How do I prepare for this in 48 hours?+
Write it from scratch twice without notes. Trace Example 1 by hand, tracking minFreq after every step. Then test capacity 1 with repeated puts on the same key. Those two traces cover nearly every bug people hit.