SevyDevy

Design an LRU cache library

PremiumLow-level designMedium

  • Low level design
  • Data structures
  • Caching

Quick answer

This low-level design interview question asks you to design an LRU cache library. Aim for a design that is modelled as clear classes, interfaces and relationships that can handle new requirements without a rewrite. Interviewers want to see how you name responsibilities, keep classes small and reason about change.

The problem

Design an in-memory key-value cache library that application developers can drop into their services. The cache has a fixed capacity. When it is full and a new key is added, it evicts the least recently used (LRU) entry. The library must be generic over key and value types, safe to use from many threads, and flexible enough to support other eviction policies later.

What must an LRU cache library do?

  • Core operations: get(key), put(key, value), remove(key), size() and clear().
  • Recency: Both get and put count as a use and make the entry the most recently used.
5 more requirementsUnlock to see all

How well must it do it?

  • Speed: get and put run in O(1) average time.
  • Concurrency: Up to 64 threads call the cache at the same time, with a target of around 1 million operations per second on one machine.
2 more requirementsUnlock to see all

What scale should you design for?

Use these numbers in your estimates.

The scale numbers unlock with this questionUnlock

Key concepts

  • Low level design
  • Data structures
  • Caching
  • Strategy pattern
  • Concurrency

How you’ll practise

6 short steps · save and come back any time
  1. Requirements
  2. Core objects
  3. Class relationships
  4. Key flows
  5. Extensibility
  6. Wrap-up

Lala, personalized to you, gives a hint when you ask and reviews your design when you are ready.

What do interviewers look for?

In any low-level design round.

  • Lists the use cases before writing classes
  • Gives each class one clear responsibility
  • Uses interfaces where behaviour is likely to change
  • Applies a design pattern only when it solves a real problem
  • Handles edge cases and invalid states explicitly
  • Walks through one use case end to end with the model

Questions people ask about this interview question

Which design type is this question?
It is a low-level design question. You design an LRU cache library and explain your choices the way you would in an interview.
Is this question asked at entry level?
It is marked Medium. Entry-level engineers can practise it, but it expects you to reason about requirements and trade-offs, not only list components.
What should I cover first?
Start with the use cases and the core entities, then the responsibilities of each one. Agree on scope with the interviewer before you design anything.
Can I practise it for free?
It is a premium question. Sign up and unlock it once for 10 credits; it then stays unlocked. When you finish, Lala, your personalized AI mentor, can review your design for 30 credits.

Related questions

Updated · Written by the SevyDevy team

Premium questionSign up free, then unlock it