FastPrepRecover a Function-Call LRU Cache with a Write-Ahead Log
Problems/Recover a Function-Call LRU Cache with a Write-Ahead Log

Recover a Function-Call LRU Cache with a Write-Ahead Log

Anthropic logoAnthropic● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Anthropic full-time onsite interview. The report asks for an LRU cache keyed by function calls, with recovery after a crash. The author says arguments are positional plus a keyword dictionary that can be serialized, and that a JSON helper is supplied. The author also says the code opens a given file path and appends log lines ending in a newline. Our version keeps the real file appends and reads. We chose these: arguments are only strings, the capacity limits, the supplied result stub, the trace of what happened, the hexadecimal record fields, and a crash model where only finished records survive. Our version is about 85% the same as the reported question.

Simulate a function-call cache with positive capacity. When adding a new entry would exceed capacity, remove the least recently used entry. Keep a write-ahead log in an actual file. Process operations in order and return one string for every operation.

Call rows

A call row is ["CALL", functionId, resultValue, positionalCount, ...positionalArgs, keywordCount, key1, value1, ...]. Counts are decimal strings and each keyword name is unique within that call. Every argument and keyword value is a string. Treat functionId as the supplied function’s identifier. Keep positional arguments in order. The order in which keyword pairs are listed does not affect the call’s identity.

A cache key is the compact JSON text of [functionId, positionalArgs, keywordDictionary], with keyword names sorted in ASCII order. Treat the serialization utility as supplied; use standard JSON string escaping. Do not join raw fields with an ambiguous delimiter.

resultValue is the supplied result of invoking that callable for those arguments, not another part of the key. It is consistent for equal keys throughout the batch. On a cache miss, invoke that supplied result stub, add one to the running invocation count that is kept outside the cache, and cache the value. On a hit, return the stored value without invoking the stub. Every call makes its key the most recently used one. Remove the least recently used key if the cache exceeds capacity. Return VALUE: followed by the value and |CALLS: followed by the cumulative count.

Actual append and recovery

The tests create one empty temporary log file and provide its file_path to the cache for the whole batch. Before applying each completed call to cache memory, open that path in append mode, write one record followed by the literal newline character, flush and close it. Log both hits and misses so recovery can restore the order of recent use.

For this exercise, a log record is hex(key) + " " + hex(value): encode the ASCII bytes as lowercase hexadecimal, put one space between fields, and append \n. An empty value has an empty second field. This framing keeps quotes, spaces and backslashes inside arguments or results separate from record boundaries.

["RECOVER"] models a cache process crash between completed calls: discard cache memory, actually open and read the same file, and replay every complete record in file order through the same LRU insertion/touch rule. Recovery does not call the underlying function or append new records. Return RECOVERED: followed by the recovered cache size and |CALLS: followed by the same count as before. Repeated recovery is allowed.

The test setup survives the simulated crash, so the call counter is not part of the recovered cache. The log is initially empty, append/read operations succeed, and crashes occur only between completed newline-terminated records. Partial writes, corrupted logs, external file deletion, concurrent callers and power-loss durability are outside the scope of this problem. Closing/flushing a file is not a claim of an fsync guarantee. Release the private temporary file after the complete batch.

Function

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

Examples

Example 1

operations = [["CALL","f","A","1","x","0"],["CALL","f","A","1","x","0"],["RECOVER"],["CALL","f","A","1","x","0"]]capacity = 2return = ["VALUE:A|CALLS:1","VALUE:A|CALLS:1","RECOVERED:1|CALLS:1","VALUE:A|CALLS:1"]

The second call is a hit, and recovery reloads the same key without invoking the underlying function.

Example 2

operations = [["CALL","f","A","1","a","0"],["CALL","f","B","1","b","0"],["CALL","f","A","1","a","0"],["RECOVER"],["CALL","f","C","1","c","0"],["CALL","f","B","1","b","0"]]capacity = 2return = ["VALUE:A|CALLS:1","VALUE:B|CALLS:2","VALUE:A|CALLS:2","RECOVERED:2|CALLS:2","VALUE:C|CALLS:3","VALUE:B|CALLS:4"]

The hit on a is logged. After recovery, b is least recent, so inserting c evicts b and a later b call is a miss.

Example 3

operations = [["CALL","f","v","1","x","2","b","2","a","1"],["CALL","f","v","1","x","2","a","1","b","2"],["CALL","g","g","1","x","2","a","1","b","2"]]capacity = 2return = ["VALUE:v|CALLS:1","VALUE:v|CALLS:1","VALUE:g|CALLS:2"]

Reordered keyword pairs form the same canonical key, while a different function ID forms a different key.

Constraints

  • 1 <= operations.length <= 2000.
  • 1 <= capacity <= 1000.
  • Each call has at most 10 positional arguments and 10 unique keyword pairs.
  • Function IDs and keyword names are nonempty strings of at most 50 characters; argument values and results contain at most 100 characters.
  • All strings contain printable ASCII characters; counts and operation shapes are valid.
  • The total length of all row strings is at most 200000.
  • Equal keys have the same supplied result. File operations are permitted in the execution sandbox.

More Anthropic interview questions

  • Longest-Match Tokenizer with Unknown SymbolsONSITE INTERVIEW · Seen Oct 2026
  • Trace Events from Stable Whole-Stack StatesONSITE INTERVIEW · Seen Oct 2026
  • Confirmed Profiling Events with First-Observed StartsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Convert Stack Samples to Trace EventsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Profiling Events from Consecutive Stack SamplesPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Repair the Bootloader ProgramOA · ONSITE INTERVIEW · Seen Sep 2026
  • Cache Shared Image Pipeline PrefixesONSITE INTERVIEW · Seen Aug 2026
  • Fair Coin from a Biased CoinPHONE SCREEN · Seen Nov 2026
  • Crawl Same-Hostname URLs ConcurrentlyPHONE SCREEN · Seen Oct 2026
  • Find Duplicate File GroupsPHONE SCREEN · Seen Sep 2026

Similar questions at other companies

  • LFU CacheAmazon · ONSITE INTERVIEW
  • LFU CacheSnowflake · OA · ONSITE INTERVIEWSnowflake interview questions
  • LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk 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
  • LFU CachexAI · ONSITE INTERVIEWxAI interview questions
  • Windowed Key-Value MapConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
  • Least Frequently Used CacheOnehouse · ONSITE INTERVIEW

Anthropic interview experiences

  • Anthropic PR Review Round with Claude Code: 2 Experiences
All Anthropic interview questionsSee Anthropic hiring insights
Saved
class Solution {
    public String[] runRecoverableCache(String[][] operations, int capacity) {
        // Write your code here.
    }
}
Checking account…