AO
Back

LRU Cache: Debugging Challenge

CodingOnsiteLast reported July 2026Low Frequency

Problem Overview

Debugging challenge — find the bug(s) in this LRU cache.

You are handed a compiling LRUCache that does not satisfy its spec, and asked to find what is wrong with it. The problem says "bug(s)" and does not say how many.

Spec:

  • get(key) returns the value for the key, or null if absent. Accessing a key makes it the most recently used.
  • put(key, value) sets or updates the value. If the cache is at capacity, evict the least recently used entry first. Inserting or updating a key makes it the most recently used.

Worked example given with the problem (capacity 2):

cache = LRUCache(2)
cache.put(1, "a")      # {1: a}
cache.put(2, "b")      # {1: a, 2: b}
cache.get(1)           # "a"  -> 1 is now most recently used
cache.put(3, "c")      # at capacity -> evicts key 2
cache.get(2)           # null (evicted)
cache.get(1)           # "a"  (still present)

The handed code is Java, backed by a LinkedHashMap<Integer, String>. The starter is a Python port of it, bugs included.

Bonus points: write a failing test case for each bug you find.

What the tests call. Fix the starter in place and keep its interface: LRUCache(capacity), get(key) returning the value or None, and put(key, value). The tests drive the fixed class through get and put only and check which keys have been evicted. Our setup — ask your interviewer: the Python port, None for null, and the test scenarios beyond the worked example.

Follow-up Arc

Interviewers escalate through these phases. The order varies, but most candidates see at least one from each bucket.
Trade-off discussion · 1
Trade-off discussion

Bonus points: write a failing test case for each bug you find.

Probes for: Stated with the problem

Practice

Write your own against 5 test cases, or read the worked solution — approach, complexity, and code that runs.

Prepping for Zip with friends?

Send them this page. It is free to read, no account needed.

More Zip Questions

Free preview

Every question in the Zip catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 14 Zip questions, refreshed monthly from new candidate reports.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
Zip · Coding · Reported 1× across candidate reports
Is this helpful?