← 返回 anthropic 的题目列表Debug an LRU Cache Implementation + Persistence After Crash (Follow-ups)
类型:online_judge
Problem: Debug an LRU Cache Implementation + Persistence After Crash (Follow-ups)
You are given a Python implementation of an LRU cache (HashMap + doubly-linked list) intended to support:
get(key): return value if present, else -1
put(key, value): insert/update; if capacity exceeded, evict the least-recently-used key
Part 1: Find the bug
The implementation has one or more logical bugs (e.g., pointer updates in the linked list, capacity bookkeeping, moving nodes on update, eviction handling, etc.).
Explain what incorrect behavior the bug causes
Propose a fix (describe code changes or provide pseudocode)
Part 2 (system/design follow-up): How to persist data after a crash?
Design a persistence mechanism so that after a process crash/restart, the cache can restore as much state as possible. Discuss:
What needs to be persisted (key/value, LRU order, capacity, metadata, etc.)
Write strategy: sync vs async writes, WAL (write-ahead log), checkpoint/snapshot
Consistency vs performance trade-offs: acceptable data loss and recovery time
Additional discussion follow-up
The interviewer may ask:
Are LRU operations CPU-bound or I/O-bound? How does that affect choosing multithreading vs async vs multiprocessing for different workloads?
Example
Input
capacity=2
put(1,1)
put(2,2)
get(1)
put(3,3)
get(2)
Output
get(1)=1; get(2)=-1 (2 should be evicted)