xAI

Verified interview challenge

Build a weighted LRU cache

Manage a cache whose capacity depends on entry size rather than the number of keys.

Unlock with Full Access

The challenge

Reads and writes update recency, while inserting or replacing a value changes the total stored size. One insertion may need to evict several older entries. Keep size accounting and recency consistent through replacements, missing reads and exact-capacity cases without scanning the whole cache for each operation.

What you'll practise

  • Capacity accounting

    Remove an entry's previous contribution before applying its replacement.

  • Recency and eviction

    Preserve the required order while making enough space for a new value.

Your coding workspacePreview
solution.pyPython 3.13
# Your implementation goes here

# Run examples and inspect results

# Full starter code included
Test cases

Run your code to see results.

Full instructions, starter code and test cases included with Full Access.