AO
Back

LRU Cache: Debugging Challenge

CodingPhone, OnsiteSoftware Engineer, Machine Learning EngineerLast reported July 2026Low Frequency

Problem Overview

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

You are handed a compiling Java LRUCache that does not satisfy its spec, and asked to find what is wrong with it.

Spec:

  • get(key) returns the value for the key, or null if absent.
Full problem statement

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

You are handed a compiling Java LRUCache that does not satisfy its spec, and asked to find what is wrong with it.

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 supplied implementation backs the cache with a LinkedHashMap created in insertion order (the given code even carries the comment // insertion order, NOT access order), moves a key to the end on get, and on eviction iterates to the last entry and removes that.

Bonus, asked explicitly: write a failing test case for each bug you find.

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

Write a failing test case for each bug you find.

Probes for: Stated with the problem

Practice

Write your own against 6 test cases, or read the worked solution — approach, complexity, and code that runs.
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 17 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?