Least Frequently Used Cache
Updated
Problem statement
Implement a least-frequently-used cache with capacity capacity. Start with an empty cache. Process each string in operations in order:
put key valueinserts a new key or updates its value. A new key starts with frequency 1. Updating a stored key increases its frequency by 1.get keyreturns its value and increases its frequency by 1 when present. Return-1when absent; a miss changes no stored frequency.
Before inserting a new key into a full cache, remove the key with the smallest frequency. If frequencies tie, remove the least recently used key among them. Every successful get, update, and insertion makes that key most recently used within its frequency. A zero-capacity cache never stores anything.
Return the results of get operations in order. Each cache operation should take average O(1) time. The finite command list drives the cache interface.
Function
processLfuCache(capacity: int, operations: String[]) → int[]Examples
Example 1
capacity = 2operations = ["put 1 10","put 2 20","get 1","put 3 30","get 2","get 3","put 4 40","get 1","get 3","get 4"]return = [10,-1,30,-1,30,40]Reading key 1 protects it from the first eviction. Before inserting key 4, keys 1 and 3 tie at frequency 2; key 1 is less recent and is evicted.
Example 2
capacity = 0operations = ["put 5 50","get 5"]return = [-1]With zero capacity, the put stores no entry.
Constraints
- For this exercise, assume
0 <= capacity <= 100000and0 <= operations.length <= 100000. - For this exercise, assume keys and values are integers in
[0, 10^9]. - Each command is exactly
put key valueorget key, with single spaces and valid integers.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
- Merge Multiple Sorted StreamsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- LFU CacheFreshworks · PHONE SCREENFreshworks interview questions
- LFU Cache with LRU Tie-BreakingRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- LFU CacheCitadel · PHONE SCREENCitadel interview questions
- LFU CacheGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Least Frequently Used Cache SimulatorAdobe · PHONE SCREENAdobe interview questions
- Windowed Key-Value MapConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- LFU CacheBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- LRU CacheAthenahealth · PHONE SCREEN
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW