FastPrepLFU Cache
Problems/LFU Cache

LFU Cache

xAI logoxAI● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report names LFU cache as a coding-round task but does not give the exact interface. Here your function gets a list of PUT and GET operations. Accesses and updates raise the frequency, and when two items tie for the lowest frequency, the least recently used one is evicted.

Implement a least-frequently-used cache with positive integer capacity. Process each command in operations in order:

  • 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.

A successful get and a put that updates an existing key each increase that key's access frequency by one and make it most recently used within its new frequency.

When insertion exceeds capacity, evict a key with the smallest frequency. If several keys share that frequency, evict the least recently used one. Return the results of the get commands in order.

Function

processLfuCache(capacity: int, operations: String[]) → int[]

Examples

Example 1

capacity = 2operations = ["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 when key 3 is inserted. Before key 4 is inserted, keys 1 and 3 have equal frequency, and key 1 is the least recently used of them.

Example 2

capacity = 1operations = ["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 to 8 and increases its frequency. Inserting key 9 into the one-entry cache then evicts key 7.

Constraints

  • 1 <= capacity <= 10000
  • 1 <= operations.length <= 200000
  • Each operation is exactly get key or put key value.
  • Every key and value fits in a signed 32-bit integer.

More xAI interview questions

  • Live Top-K Spaces by Active UsersONSITE INTERVIEW · Seen Jan 2026
  • Total Active Time by SpaceONSITE INTERVIEW · Seen Jan 2026
  • Dynamic Batch Decode TraceONSITE INTERVIEW · Seen Dec 2025
  • Nested Transaction Key-Value StoreONSITE INTERVIEW · Seen Oct 2025
  • Weighted LRU CacheONSITE INTERVIEW · Seen Oct 2025
  • Cached Lazy Token BucketsONSITE INTERVIEW · Seen Sep 2025
  • Least Recently Used CacheONSITE INTERVIEW · Seen Jun 2025
  • Resumable List IteratorPHONE SCREEN · Seen Apr 2026
  • Earliest Dataset Readiness in a PipelinePHONE SCREEN · Seen Mar 2026
  • Largest Plus SignOA · Seen May 2025

Similar questions at other companies

  • LFU CacheAmazon · ONSITE INTERVIEW
  • LFU CacheSnowflake · OA · ONSITE INTERVIEWSnowflake interview questions
  • LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk interview questions
  • Recover a Function-Call LRU Cache with a Write-Ahead LogAnthropic · ONSITE INTERVIEWAnthropic interview questions
  • LFU CacheSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • LFU CacheBlinkit · ONSITE INTERVIEWBlinkit interview questions
  • Fair Multi-Tenant CacheOkta · ONSITE INTERVIEWOkta interview questions
  • LFU CacheZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions
  • Windowed Key-Value MapConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
  • Least Frequently Used CacheOnehouse · ONSITE INTERVIEW

xAI interview experiences

  • xAI New Grad AI Coding Interview and Take-Home: 3 Experiences
All xAI interview questionsSee xAI hiring insights
Saved
class Solution {
    public int[] processLfuCache(int capacity, String[] operations) {
      // write your code here
    }
}
Checking account…