Reported September 2026
Microsoftdesign

Weighted LFU Cache

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

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

Microsoft reported this one in September 2026, and it's an LFU cache with a twist: every entry has a size, and eviction runs in a loop until total size fits. If you've seen LeetCode's LFU Cache, you know the skeleton. The weights and the PEEK command are where people slip. The whole solution hinges on one structure: a hash map from key to entry, plus frequency buckets that each keep entries in recency order. Get that right and every operation is O(1) amortized. Get it wrong and 2 * 10^5 queries will punish you. If you freeze on the live OA, StealthCoder is the safety net that reads the problem and hands you a working design.

The problem

Implement a Weighted Least Frequently Used (LFU) Cache with total capacity capacity. Every cached entry has an integer key, integer value, positive integer size, access frequency, and recency. The sum of the sizes of all cached entries must never exceed capacity.
Process the strings in queries in order. Each query has one of these forms:
PUT key value size: insert or update an entry. If size > capacity, ignore the entire operation and leave the cache unchanged. For an existing key, update its value and size without changing its frequency, and mark it as most recently accessed within that frequency. For a new key, insert it with frequency 1 and make it the most recent entry at that frequency.
GET key: if the key is absent, append -1 to the result. Otherwise, append its value, increment its frequency by 1, and make it the most recently accessed entry at the new frequency.
PEEK key: append the key's value when it exists, or -1 otherwise, without changing its frequency or recency.
After every accepted PUT, while the total stored size exceeds capacity, evict an entry with the smallest frequency. If several entries share that frequency, evict the least recently accessed one. The eviction policy considers every current entry, including the key just inserted or updated.
Return the results of all GET and PEEK queries in their original order.

Function
processWeightedLfuCache(capacity: int, queries: String[]) → int[]

Examples
Example 1
capacity = 10
queries = ["PUT 1 100 4","PUT 2 200 4","PEEK 1","GET 1","PUT 3 300 5","PEEK 2","GET 2","PEEK 3"]
return = [100,100,-1,-1,300]
PEEK 1 returns 100 without changing state. GET 1 then raises key 1 to frequency 2. Inserting key 3 exceeds capacity, so key 2 is evicted as the least-recent entry among the keys with frequency 1.
Example 2
capacity = 5
queries = ["PUT 1 10 2","PUT 2 20 2","PEEK 1","PUT 3 30 2","GET 1","GET 2","PEEK 3"]
return = [10,-1,20,30]
PEEK 1 does not refresh recency. When key 3 is inserted, key 1 is still the least-recent entry among those with frequency 1, so it is evicted.

Constraints
1 <= capacity <= 10^9
1 <= queries.length <= 2 * 10^5
Every query is exactly GET key, PEEK key, or PUT key value size.
Every key and value fits in a signed 32-bit integer.
1 <= size <= 10^9 for every PUT query.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two maps. One maps key to its entry (value, size, frequency). The other maps frequency to an ordered collection of keys, oldest first, and you track the minimum frequency. In Python or Java, an OrderedDict or LinkedHashSet per frequency does it. GET moves a key from bucket f to bucket f+1 at the most recent end. PEEK touches nothing. That's the classic pitfall, so don't refresh recency there. PUT on an existing key updates value and size, keeps frequency, and moves it to the recent end of its bucket. PUT with size > capacity is ignored entirely, even for an existing key. After an accepted PUT, loop: while total size > capacity, evict the oldest key in the minimum-frequency bucket. That can evict the key you just inserted. Track total size as a running sum and recompute the minimum frequency after evictions. If you blank during the real OA, StealthCoder is the hedge that gives you the bucket structure fast.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Weighted 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Microsoft reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Weighted LFU Cache FAQ

How hard is the Weighted LFU Cache problem really?+

It's a hard-ish design problem, but it's LeetCode 460 plus weights. If you can write the standard LFU with frequency buckets, the extra work is a running total size and an eviction loop. Most failures come from small rule details, not the data structure.

What's the trick to getting O(1) per operation?+

Keep a key-to-entry map and a frequency-to-ordered-keys map, plus a minimum frequency pointer. Ordered buckets give you the least recently used key at a given frequency instantly. Each GET moves one key up one bucket, and each eviction pops one key from the front.

Does PEEK change frequency or recency?+

No. PEEK only returns the value or -1. It must not bump frequency or refresh recency. Example 2 tests exactly this: after PEEK 1, key 1 is still the oldest at frequency 1 and gets evicted when key 3 arrives.

What happens when a PUT has size larger than capacity?+

The whole operation is ignored and the cache stays unchanged. That includes an existing key, so you don't update its value or size and you don't evict anything. Check this first, before touching any state.

How do I prepare for this in 48 hours?+

Write the standard LFU cache from scratch once, with bucketed ordered maps. Then add size tracking and a while-loop eviction. Trace both examples by hand, especially the PEEK case and the case where the just-inserted key gets evicted itself.

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

OA at Microsoft?
Invisible during screen share
Get it