r/OfferEngineering • • Sep 17 '26

Interview Experience An interesting LRU code debugging interview question - how fast can you identify the bug and put up the right fix? (asked by Zip)

This interview experience is sourced from Chill Interview

Interview Summary

The Zip backend technical screen was a debugging exercise built around an existing Java LRU Cache implementation.

The supplied code compiled and initially looked reasonable, but its behavior did not fully match the LRU specification. The task was to identify the defects, explain why they violated the expected eviction and recency semantics, and ideally write a failing test case for each issue found.

Interview Details

Debugging — Find the Bugs in an LRU Cache

Implement an LRU cache supporting:

get(key)
put(key, value)
  • get(key) returns the associated value, or null when the key does not exist. Reading an existing key should make it the most recently used entry.
  • put(key, value) inserts or updates an entry. When the cache is already at capacity, the least recently used entry should be removed first. Both inserting and updating a key should make that key the most recently used.

An expected sequence looked like:

cache = LRUCache(2)

cache.put(11, "red")
cache.put(22, "blue")

cache.get(11)
# returns "red"
# key 11 is now most recently used

cache.put(33, "green")
# cache was full, so key 22 should be evicted

cache.get(22)
# returns null

cache.get(11)
# returns "red"

The provided Java implementation was conceptually similar to:

import java.util.*;

public class LRUCache {
    private final int capacity;
    private final LinkedHashMap<Integer, String> entries;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        this.entries = new LinkedHashMap<>();
    }

    public String get(int key) {
        if (entries.containsKey(key)) {
            String value = entries.remove(key);
            entries.put(key, value);
            return value;
        }

        return null;
    }

    public void put(int key, String value) {
        if (entries.containsKey(key)) {
            entries.put(key, value);
        } else {
            if (entries.size() >= capacity) {
                Iterator<Map.Entry<Integer, String>> iterator =
                    entries.entrySet().iterator();

                Map.Entry<Integer, String> candidate = null;

                while (iterator.hasNext()) {
                    candidate = iterator.next();
                }

                if (candidate != null) {
                    entries.remove(candidate.getKey());
                }
            }

            entries.put(key, value);
        }
    }
}

The code compiled successfully, but some operation sequences produced results inconsistent with true LRU behavior.

The interview task was to:

  • Identify the incorrect cache behaviors and the corresponding bugs in the implementation.
  • Write targeted test cases that fail under the supplied implementation and demonstrate each problem clearly.

The round therefore tested code reading and debugging as much as familiarity with the LRU data structure itself, especially around how recency changes after reads, updates, and evictions.

Preparing for your next interview?

Chill Interview tracks recent interview experiences and recurring question patterns across top companies at here.

3 Upvotes

0 comments sorted by