NVIDIA interview question
Implement a least-frequently-used cache with O(1) operations
- Company:
- NVIDIA
- Type:
- Coding
- Difficulty:
- Hard
- Reported:
- November 3, 2026
Problem
Implement an LFU (least frequently used) cache with a fixed capacity:
get(key): return the value if the key exists, otherwise-1. This counts as a use of the key.put(key, value): insert or update the key (an update counts as a use). When inserting a new key into a full cache, first evict the key with the smallest use count; if several keys tie, evict the least recently used among them.
Both operations should run in O(1) average time.
Example 1
Input (capacity 2): put(1,10), put(2,20), get(2), put(3,30), get(1), get(2), get(3)
Output: 20, -1, 20, 30
Key 1 has the lowest count when 3 arrives, so it is evicted.
Example 2
Input (capacity 1): put(1,1), get(1), put(2,2), get(1), get(2)
Output: 1, -1, 2
With capacity 1, inserting 2 always evicts the only key.
Constraints
- get and put in O(1) average time.
Reported by candidates or drawn from publicly available sources, and rewritten in our own words. Pathwise Prep does not publish confidential assessment content. Report a concern