LRU Cache With Pinned Entries for Shared Prompt Prefixes
Last updated: November 29, 2026
Quick Overview
Design an LRU cache with get, put, pin and unpin where pinned entries (reference counted) can never be evicted, and put on a full cache of only pinned entries is rejected rather than evicting.
OpenAI
November 29, 20260
0
0 solved
Design an LRU cache with get, put, pin and unpin where pinned entries (reference counted) can never be evicted, and put on a full cache of only pinned entries is rejected rather than evicting.
Inference servers cache attention key-value blocks for shared prompt prefixes, and blocks in use by an in-flight request must not be evicted, which maps directly to an LRU with pinning. OpenAI interviewers use this to see whether you can extend a familiar structure with reference counts while keeping every operation O(1) and making the full-cache behavior explicit.
What the Interviewer Expects
- Keep get, put, pin and unpin all O(1)
- Use reference counts so multiple holders can pin the same key
- Exclude pinned entries from eviction without scanning
- Define behavior when capacity is exhausted by pinned entries
- Decide where an unpinned entry re-enters the LRU order and justify it
- Test multi-pin, unpin and eviction order
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Entries have different sizes (bytes): how does eviction change?
- Make it thread-safe with minimal lock contention.
- Blocks form a prefix tree; evicting a parent must not orphan pinned children. How do you enforce that?
- How would you expose hit rate and eviction metrics?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
get(key): value orNone; marks the key recently used.put(key, value): insert or update; evict the least recently used unpinned entry if full.pin(key): increment a pin count; pinned key...
High-Level Design
Split entries into two containers. Unpinned entries live in an OrderedDict that encodes LRU order, so eviction is popitem(last=False) in O(1). Pinned entries live in a plain dict with `[value, cou...