Code Room
CodingHardcod-g778
Subject Design data structuresLevel Senior–Staff~40 minCommon in Algorithms & data structures interviewsIndustries Software development, Technology

Question

Implement an LFU (least-frequently-used) cache with capacity `cap`. ['put', key, val] inserts/updates; ['get', key] returns the value or -1 if absent. Both get and put count as a use that increments the key's frequency. When at capacity and inserting a new key, evict the least-frequently-used key; if several tie on frequency, evict the least-recently-used among them. Return the list of values returned by each 'get'. If cap is 0, nothing is ever stored.

Implement
lfu_cache(cap: int, ops: list[list]) → list[int]
Examples
in[2,[["put",1,1],["put",2,2],["get",1],["put",3,3],["get",2],["get",3],["put",4,4],["get",1],["get",3],["get",4]]]out[1,-1,3,-1,3,4]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.