BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / LLM inference and serving36 of 36 in topic

827. Build an LRU cache for model responses

MediumLLM inference and serving

Why it matters LRU caches front LLM endpoints, embedding services and KV block managers, and the problem is a staple of coding interviews.

An LRU (least recently used) cache keeps the most recently used responses and evicts the one untouched for longest when it is full. It is one of the most asked coding interview problems and sits in front of many LLM endpoints.

Write run_lru(capacity, ops). ops is a list of ("put", key, value) or ("get", key). A get returns the value or None and marks the key as most recently used. A put inserts or updates the key, marks it most recently used, and if the cache now holds more than capacity keys, evicts the least recently used one. Return the list of results of the get operations in order. Every operation must be O(1) on average.

Functiondef run_lru(capacity, ops)

Example 1

Input: run_lru(2, [('put', 'a', 1), ('put', 'b', 2), ('get', 'a'), ('put', 'c', 3), ('get', 'b'), ('get', 'c')])

Output: [1, None, 3]

Example 2

Input: capacity = 1, ops = [('put', 1, 'x'), ('put', 1, 'y'), ('get', 1)]

Output: ['y']

Example 3

Input: capacity = 2, ops = [('put', 1, 1), ('put', 2, 2), ('put', 1, 10), ('put', 3, 3), ('get', 2), ('get', 1)]

Output: [None, 10]

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Your code must pass all 5 tests to be accepted.
Hint 1

A dict alone cannot tell you which key is oldest; you also need an order.

Hint 2

OrderedDict.move_to_end and popitem(last=False) are both O(1).

Hint 3

Updating an existing key also counts as a use.

Common mistakes
  • Scanning a list to find the oldest key, which is O(n) per operation and too slow for 100,000 puts.
  • Forgetting that get also refreshes the key's position.
Understand the idea

You need fast lookup (a hash map) and fast reordering (a doubly linked list). Python's OrderedDict is both: move_to_end marks use and popitem(last=False) removes the oldest.

Worked example

capacity 2, put a, put b, get a (a is now newest), put c evicts b, so get b is None and get c is 3.

get and put both move the key to the end; evict from the front when over capacity
Topics
LLM inference and serving
Similar questions
  • Generate one token at a time with a KV cacheMedium
  • Allocate KV cache memory in pages, like vLLMMedium
  • Verify a draft model's guesses in speculative decodingMedium
  • Is decoding memory-bound? A roofline estimateMedium
All LLM inference and serving problems
Report
827 / 903
Python 3solution.pyPython idle
1
2
3
4
5
from collections import OrderedDict
def run_lru(capacity, ops):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 5 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS