FastPrepWeighted LRU Cache
Problems/Weighted LRU Cache

Weighted LRU Cache

xAI logoxAI● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report describes an LRU cache in which each item has a quantity and capacity is measured by the sum of those quantities. This version calls that quantity weight and defines how operations are written, what an update does, the output for a miss, and the limits.

Process operations against an initially empty cache with total weight capacity capacity.

  • ["PUT", key, value, weight] inserts or replaces an item and makes it most recently used.
  • ["GET", key] returns the value and makes a present item most recently used; a miss returns NULL.

After every PUT, while the sum of resident item weights exceeds capacity, evict the least recently used item. Replacing a key removes its previous weight before adding the new item. A single insertion may therefore evict multiple items.

Return the outputs of GET operations in order.

Function

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

Examples

Example 1

capacity = 5operations = [["PUT","a","A","3"],["PUT","b","B","2"],["GET","a"],["PUT","c","C","4"],["GET","a"],["GET","c"]]return = ["A","NULL","C"]

The hit on a makes b least recent. Adding weight-4 c first evicts b, then still exceeds capacity and evicts a.

Example 2

capacity = 5operations = [["PUT","a","old","2"],["PUT","b","B","3"],["PUT","a","new","1"],["PUT","c","C","2"],["GET","b"],["GET","a"],["GET","c"]]return = ["NULL","new","C"]

Replacing a changes its weight from 2 to 1 and refreshes its recency. Adding c then evicts b.

Constraints

  • 1 <= capacity <= 10^9.
  • 1 <= operations.length <= 200000.
  • Each weight is an integer in [1, capacity].
  • Keys and values are non-empty ASCII strings; values are never NULL.
  • The sum of all operation-string lengths is at most 2000000.

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
  • LFU CacheONSITE INTERVIEW · Seen Sep 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

  • LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
  • LRU CacheLotusFlare · ONSITE INTERVIEW
  • LRU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
  • LRU Cache OperationsHevo Data · ONSITE INTERVIEW
  • LRU Cache OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • LRU Cache Operation SequenceGoogle · ONSITE INTERVIEWGoogle interview questions
  • Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
  • LRU CacheAMD · ONSITE INTERVIEWAMD interview questions

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 String[] weightedLru(int capacity, String[][] operations) {
        // Write your code here.
    }
}
Checking account…