Skip to the page

Classic problems · mid

Cache, key-value store, rate limiter and logger

These problems are data structures plus concurrency. Know LRU with a map and a doubly linked list, expiry with a clock, the rate limit algorithms, and a fast logging path.

The notes

What you must know

  • Cache: interface Cache with get and put, and an EvictionPolicy interface (keyAccessed, keyAdded, evict) as a Strategy so LRU, LFU and FIFO plug in.
  • LRU: a hash map plus a doubly linked list with sentinel head and tail. On access move the node to the front and evict from the tail. All steps O(1). LFU in O(1): key to node, frequency to a list of nodes, and a minFreq counter.
  • Edge cases: capacity 0 or 1, put on an existing key updates the value and counts as an access, get on a missing key. Get changes the order, so it is a write: a read-write lock does not help. One lock is correct; segment by key hash for speed.
  • Key-value store with TTL: an Entry with value and expiresAt, and a Clock. Lazy check on get (remove only if the entry is still the same, map.remove(key, entry)) plus active cleanup with a min-heap or DelayQueue or random sampling. Use fake clocks in tests.
  • Rate limiter: interface RateLimiter allow(clientId). Token bucket: capacity and refill rate, refill lazily on each call by elapsed time, no background thread. Fixed window allows 2 times bursts at the boundary. Sliding log is exact but stores every timestamp. Sliding counter approximates.
  • State per client in a ConcurrentHashMap, with refill-and-take atomic on each bucket. Never one global lock for all clients. Evict idle buckets.
  • Logging library: LogLevel, LogRecord, Logger, Appender and Formatter interfaces, a LoggerFactory. Check the level first and take a lazy message supplier. An async appender uses a bounded queue drained by a background thread; state the full-queue policy. A volatile level field allows changes at run time. Flush on shutdown.
  • Bad ideas: an array scan that makes eviction O(n), a cleanup thread scanning the whole map every second, one thread per client to refill tokens, and synchronized on every log call that writes to a file.

The red pen

Common traps: mistake → fix

  • Mistake: A read lock is enough for LRU get

    Fix: get moves the node, so it writes.

  • Mistake: A singly linked list is fine for LRU

    Fix: you cannot remove a node in O(1).

  • Mistake: Fixed window is exact

    Fix: it allows double bursts at the boundary.

  • Mistake: Building the log string before checking the level is harmless

    Fix: it wastes time when the level is off.

Easy to hard

Questions interviewers ask, easy to hard

  1. Design an LRU cache with O(1) get and put.
  2. Now make it thread-safe. What do you lock?
  3. Design a key-value store with TTL. How does expiry work?
  4. Design a rate limiter. Compare token bucket and fixed window.
  5. How do you make a logging library cheap when disabled?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview