# Implement LRU Cache — Aced (formerly Exponent)

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/3625/lru-cache
Source observed: 2026-11-04T22:21:36.586Z
Last checked: 2026-11-04T22:21:36.586+00:00
Indexed: 2026-11-05T19:37:06.094+00:00

---

# Implement LRU Cache

**Difficulty:** Hard  
**Category:** Data Structures & Algorithms  
**Reported interviews:** 68  
**Last asked:** 5 days ago (as displayed when we captured the page)  
**Community:** 45  
**Language:** Python

We provide this coding interview question for implementing a least recently used (LRU) cache. The scenario is a program that repeatedly reads data from disk. The cache stores key-value pairs in memory while limiting its size to `N` items.

## Task

Write a class `LRUCache(n)` that accepts a size limit `n` and supports:

- `set(key, value)` to insert or update an item
- `get(key)` to retrieve an item

Both methods should run in **`O(1)` time**.

```python
class LRUCache(n):
    set(key, value)
    get(key)
```

## Examples

When the cache is full, we remove the least recently used item to make room for a new item. An item is considered used whenever it is set, retrieved, or updated.

```python
cache = LRUCache(2) # Limit of 2 items
cache.set('user1', 'Alex')
cache.set('user2', 'Brian')
cache.set('user3', 'Chris')
```

`user1` is removed because it was the least recently used item when `user3` was inserted:

```python
cache.get('user1') # => None
cache.get('user2') # => 'Brian'
cache.get('user3') # => 'Chris'
```

The page includes four expandable hints and one challenge, plus tabs for the question, a locked solution, and community content. We also provide an in-page Python editor with Output, Test Results, and Console panels; running code requires [signing up](https://www.aced.io/signup?src=lesson).

## Interview experiences

We show 34 shared interview experiences related to this question. Each entry below is a user-submitted account, retained as an example of interview preparation material.

- [Amazon · 5 days ago](https://www.aced.io/experiences/amazon-software-engineer-interview-5766eb) — Software Engineer, Entry Level / L3: “The process seems to be getting more rigorous than the past, with multiple technical rounds. There is a large focus on the leadership principles.”
- [Qualys · 2 months ago](https://www.aced.io/experiences/qualys-software-engineer-interview-5f6cb1) — Software Engineer, Entry Level / L3: The interview included HR discussion, system design for an event-streaming system, project and sharding questions, Spring internals, and implementing an LRU cache, including logic edge cases and multithreaded behavior.
- [LinkedIn · 2 months ago](https://www.aced.io/experiences/linkedin-senior-software-engineer-interview-5dcc7c) — Software Engineer, Senior / L5: The contributor reported that coding and system design went smoothly.
- [Google · 4 months ago](https://www.aced.io/experiences/google-senior-software-engineer-interview-c8621f) — Software Engineer, Senior / L5: The account describes recruiter, technical, onsite or virtual, coding/DSA, system design, behavioral, leadership, and Googleyness or culture-related stages.
- [Apple · 5 months ago](https://www.aced.io/experiences/apple-senior-software-engineer-interview-d9a543) — Software Engineer, Senior / L5: The contributor described a long, unclear process and rushed technical rounds.
- [Meta · 5 months ago](https://www.aced.io/experiences/meta-facebook-senior-software-engineer-interview-b893a3) — Software Engineer, Senior / L5: One phone screen was followed by a four-round loop containing two coding rounds, one behavioral round, and one system-design round.
- [IBM · 5 months ago](https://www.aced.io/experiences/ibm-software-engineer-intern-interview-92a8e2) — Software Engineer, Intern: The account describes LeetCode medium questions, a behavioral interview, and a reported offer timeline.
- [Google · 6 months ago](https://www.aced.io/experiences/google-software-engineer-intern-interview-995055) — Software Engineer, Intern: The contributor said they felt underprepared but considered it a good experience.
- [Amazon · 6 months ago](https://www.aced.io/experiences/amazon-software-engineer-interview-9fe6ee) — Software Engineer: The panel was team-specific and included three technical/behavioral rounds, including a bar-raiser round and questions about generative AI.
- [Stripe · 6 months ago](https://www.aced.io/experiences/stripe-senior-software-engineer-interview-af32ea) — Software Engineer, Senior / L5: The contributor described practical technical discussions, behavioral and problem-solving coverage, and some uncertainty around scheduling and next steps.
- [Amazon · 6 months ago](https://www.aced.io/experiences/amazon-software-engineer-intern-interview-10f679) — Software Engineer, Intern: The account describes two one-hour rounds, each split between behavioral and technical discussion, with typical coding questions and AI-related questions.
- [Microsoft · 7 months ago](https://www.aced.io/experiences/microsoft-software-engineer-interview-3807a1) — Software Engineer, Mid Level / L4: The technical interviews used real-world code implementations rather than direct LeetCode-style questions; the system-design round had open-ended, ambiguous requirements.
- [Microsoft · 7 months ago](https://www.aced.io/experiences/microsoft-senior-software-engineer-interview-803fed) — Software Engineer, Senior / L5: The contributor applied through Microsoft’s portal and was also contacted by a recruiter on LinkedIn.
- [Microsoft · 7 months ago](https://www.aced.io/experiences/microsoft-senior-software-engineer-interview-e2b773) — Software Engineer, Senior / L5: The contributor described being contacted after applying and interviewing for a senior software-engineer role.
- [GitHub · 7 months ago](https://www.aced.io/experiences/github-software-engineer-intern-interview-1e2a47) — Software Engineer, Intern: The process was described as friendly but lengthy, taking four months end to end.
- [Walmart Labs · 7 months ago](https://www.aced.io/experiences/walmart-labs-software-engineer-interview-5e1b59) — Software Engineer, Mid Level / L4: The contributor was not prepared for the low-level-design interview and spent too much time on the first data-structures question.
- [Meta · 8 months ago](https://www.aced.io/experiences/meta-facebook-software-engineer-intern-interview-5c514a) — Software Engineer, Intern: The initial interview covered a self-introduction, the most challenging project, and desired work at Meta; the contributor reported receiving the pass notification after an hour.
- [Microsoft · 8 months ago](https://www.aced.io/experiences/microsoft-software-engineer-interview-a483e9) — Software Engineer, Mid Level / L4: The account again describes real-world code implementation interviews and an open-ended system-design round.
- [Apple · 8 months ago](https://www.aced.io/experiences/apple-devops-interview-3f2300) — DevOps: The contributor prepared stories involving Linux, Kubernetes, Docker, Terraform, and GitHub Actions; live Python coding under time pressure was the harder part.
- [Anthropic · 10 months ago](https://www.aced.io/experiences/anthropic-senior-software-engineer-interview-2ffa5f) — Software Engineer, Senior / L5: The account describes a hard pre-assessment followed by five onsite rounds: hiring manager, coding, company values, system design, and another coding round. The contributor was still waiting for an outcome at the time of the account.
- [Confluent · a year ago](https://www.aced.io/experiences/confluent-software-engineer-interview-6d0f1d) — Software Engineer, Mid Level / L4: The process included two LeetCode rounds, two system-design rounds, and one behavioral round; the contributor described coding difficulty as medium to hard.
- [Meta · a year ago](https://www.aced.io/experiences/meta-facebook-software-engineer-interview-1abd5a) — Software Engineer, Entry Level / L3: Both technical rounds used questions from a top-50 Meta-tagged LeetCode list, with a standard behavioral round.
- [Accenture · a year ago](https://www.aced.io/experiences/accenture-data-engineer-interview-932289) — Data Engineer, Mid Level / L4: The account covered data engineering, data warehousing, ETL pipelines, large-scale processing, technical discussions, and behavioral experiences.
- [xAI · a year ago](https://www.aced.io/experiences/x-ai-software-engineer-interview-4dda03) — Software Engineer: The process was iterative, with practical questions about coding, abuse prevention, UX, latency, and monetization, alongside scheduling changes and multiple hiring-manager conversations.
- [Microsoft · a year ago](https://www.aced.io/experiences/microsoft-software-engineer-interview-a7a9e5) — Software Engineer, Mid Level / L4: The contributor said they struggled with implementing an LRU cache because they had not seen the question before.
- [Nvidia · a year ago](https://www.aced.io/experiences/nvidia-senior-software-engineer-interview-5768ad) — Software Engineer, Senior / L5: The process included recruiter and coding screens, a hiring-manager chat, and a four-round virtual onsite focused on memory, compute, parallelism, GPU utilization, LLM fundamentals, and large-scale distributed training.
- [xAI · 2 years ago](https://www.aced.io/experiences/x-ai-software-engineer-interview-2fe9c1) — Software Engineer: The account describes an engineer screen, two coding interviews, and a canceled design round; the first coding round involved an LRU cache.
- [GitHub · 2 years ago](https://www.aced.io/experiences/github-software-engineer-intern-interview-8c2826) — Software Engineer, Intern: The contributor described friendly interviewers, manageable questions, and straightforward manager and behavioral discussions.
- [Temenos · 2 years ago](https://www.aced.io/experiences/temenos-senior-software-engineer-interview-551857) — Software Engineer, Senior / L5: The process covered Core Java, Spring Boot, microservices, system design, databases, and banking and enterprise-software experience.
- [Robinhood · 2 years ago](https://www.aced.io/experiences/robinhood-software-engineer-interview-03c8b5) — Software Engineer, Entry Level / L3: The contributor described the process as easy when prepared.
- [Meta · 3 years ago](https://www.aced.io/experiences/meta-facebook-senior-software-engineer-interview-8f6473) — Software Engineer, Senior / L5: The account focused on LeetCode-style coding and abstract design prompts such as Netflix or Instagram; the contributor said the candidate drives the design discussion and that the round affects leveling.
- [Compass · 3 years ago](https://www.aced.io/experiences/compass-software-engineer-interview-ce9cb5) — Software Engineer: Interviewers were described as helpful and expecting candidates to communicate their thought process.
- [Amazon · 3 years ago](https://www.aced.io/experiences/amazon-senior-software-engineer-interview-5f3e3b) — Software Engineer, Senior / L5: The contributor described a smooth interview with direct questions.
- [Microsoft · 3 years ago](https://www.aced.io/experiences/microsoft-software-engineer-interview-27950b) — Software Engineer: The account covered an introduction, a challenging past project, and high-level design of a configuration service for updating configurations across multiple microservices.

## Related questions

- [Implement Trie](https://www.aced.io/questions/3441/implement-trie)
- [Implement mutex](https://www.aced.io/questions/7283/implement-mutex)
- [Design YouTube, then implement the cache layer.](https://www.aced.io/questions/3713/design-youtube-implement-cache-layer)

## Related courses

### [AI Engineering Interview Prep](https://www.aced.io/courses/ai-engineering)

![AI Engineering Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/25mhKSvDOiCwC89lDj89Ah/9a5fbfacc8d93f05c238ab55baa4e5e5/fde.png?w=800&h=800&fm=webp&q=75)

Prepare for AI-engineering interviews at companies such as Google, Anthropic, Meta, and OpenAI with frameworks, example questions, mock interviews, system design, AI/ML concepts, AI-assisted coding, and behavioral-round practice.

### [Generative AI Interviews](https://www.aced.io/courses/gen-ai-interviews)

![Generative AI Interviews](https://images.ctfassets.net/x78yjrjc11pq/BLBU7an1PjT0Mt2ZzAfDa/60e119c8bff0cfb8281dcb497177f6e3/AdobeStock_1510292201-min.jpeg?w=800&h=800&fm=webp&q=75)

Prepare for interviews at AI-first and AI-powered companies. Learn to explain how generative AI works, where it fails, and how to use it responsibly, including LLMs, retrieval-augmented generation (RAG), and production deployment.

## Navigation and preparation resources

We provide [all questions](https://www.aced.io/questions), interview experiences, practice, mock interviews and coaching, resume review, salary negotiation, [pricing](https://www.aced.io/upgrade?src=nav), and [job referrals](https://www.aced.io/jobs/referrals?src=nav). Aced is formerly Exponent.

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F3625%2Flru-cache&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F3625%2Flru-cache). Opening a form does not execute a tool.