Skip to main content
All Questions

Implement LRU Cache.

Hard

Your program is running slowly because it's accessing data from disk over and over again. To improve the performance, you want to build a simple key-value store to cache this data in memory, but you also want to limit the amount of memory used. You decide to build a caching system that only keeps the N most recently used items—also known as a least recently used (LRU) cache.

Write a class LRUCache(n) that accepts a size limit n. It should support a set(key, value) method for inserting or updating items and a get(key) method for retrieving items. Can you implement a solution where both of these methods run in O(1) time?

Python
class LRUCache(n): set(key, value) get(key)

Examples

To make room for new items, the least recently used item should be removed. An item is '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')

Here user1 is empty because it was the least recently used and thus removed to make room for user3:

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

Interview experiences

22 shared
Google · 2 months ago
"few stages: an initial recruiter conversation, a technical screening, and then a set of onsite or virtual interviews. The technical rounds typically cover coding/DSA, system design, and behavioral or leadership-oriented questions. For a Senior role, I’d expect system design and demonstrating technical leadership to carry significant weight. I’d also prepare for Googleyness/culture-related questions"
Software Engineer · Senior / L5
IBM · 3 months ago
"The process was really easy just leetcode mediums then a behavioral. I solved the two problems, and then two weeks later got an interview. A week after the interview you should get the offer."
Software Engineer · Intern
Google · 4 months ago
"i think i definitely underprepared so i am preparing a lot leading up to my nexxt interview it was a good experience"
Software Engineer · Intern
Amazon · 4 months ago
"My interview panel was for a specific team and not a general hiring pipeline. 3 rounds technical + behavioral. First 20 mins behavioral then technical. Last round was bar raiser. Everything went good for me, few questions were new like "experience with gen ai", rest everything was traditional LC + LP based behavioral."
Software Engineer
Microsoft · 5 months ago
"The process was quick with all interview rounds initially scheduled in the same week. The technical interviews were adapted to real world code implementations and didn't directly relate to Leetcode style problems. The system design round was very open-ended with no definite question and highly ambiguous requirements."
Software Engineer · Mid Level / L4
Microsoft · 5 months ago
"Directly applied on their portal, Recruiter reached out to me directly on LinkedIn as well and I got interviewed by Microsoft or a Senior Software engineer role in March end and here's the experience"
Software Engineer · Senior / L5
Microsoft · 5 months ago
"Directly applied on their portal, Recruiter reached out to me directly on LinkedIn as well and I got interviewed by Microsoft for a senior software engineer role in April."
Software Engineer · Senior / L5
Meta · 6 months ago
"The interview began with a brief self-introduction, followed by two questions: Most challenging project. What do you want to work on at Meta? The discussion lasted exactly 15 minutes. The interviewer did not interrupt at any point. Notification of passing the initial interview was received after an hour."
Software Engineer · Intern
Microsoft · 6 months ago
"The process was quick with all interview rounds initially scheduled in the same week. The technical interviews were adapted to real world code implementations and didn't directly relate to Leetcode style problems. The system design round was very open-ended with no definite question and highly ambiguous requirements."
Software Engineer · Mid Level / L4
Anthropic · 8 months ago
"I did a hard pre-assessment first, then the recruiter fast-tracked me because I already had other offers and got me through the rest of the loop in about a week. The onsite was five rounds for me: hiring manager, coding, company values, system design, and then another coding round. The most unusual part was the company values interview, which was run by nontechnical people and felt almost like a therapy session because they kept probing how I felt, not just what I did. The technical rounds were more practical than I expected, while the behavioral side was where I felt the real pressure. I finished the loop, but at the time of this conversation, I was still waiting on the outcome."
Software Engineer · Senior / L5
Accenture · 10 months ago
"Overall, the interview process was well organized and professional. The interviewers were engaged, respectful of time, and provided opportunities to discuss both technical and behavioral experiences. I appreciated the focus on real-world problem solving and the chance to explain my thought process rather than just provide answers. What went well was my ability to communicate my experience with data engineering, data warehousing, ETL pipelines, and large-scale data processing. I was also able to provide detailed examples from past projects and relate them to the requirements of the role. One area that could have gone better was managing time during some of the technical discussions. In a few cases, I spent more time than necessary exploring alternative approaches before arriving at the final solution. Looking back, I could have structured my responses more concisely and clarified assumptions earlier. Overall, the process was a valuable experience that helped me better understand the role, the team’s expectations, and the areas where I can continue to improve"
Data Engineer · Mid Level / L4
Meta · 10 months ago
"The process was very straightforward. In both technical rounds, I was asked questions from the top 50 Meta tagged LC questions list. Behavioral was also very standard."
Software Engineer · Entry Level / L3
xAI · 10 months ago
"I was pulled in directly by a recruiter, and the loop was iterative: each round only unlocked if I passed the previous one. The interviews themselves were pretty simple on the surface, but they were wide and very practical: a small coding problem, then straight into real questions about abuse prevention, UX, latency, and monetization. The messy part was the operations side. Schedules kept moving, and company policy changed. I ended up doing extra late-night calls and two hiring manager conversations because the manager changed in the middle. Even with that chaos, it was one of the most efficient big-company processes I've had, and I ended up signing the offer after a very direct comp conversation."
Software Engineer
Microsoft · a year ago
"The process was smooth, the interviewers were helpful. I messed implmenting LRU cache as I had never seen that question before."
Software Engineer · Mid Level / L4
Nvidia · a year ago
"I went through a pretty team-specific process for a Senior Software Engineer, LLM Applications role: recruiter screen, coding screen, short hiring manager chat, then a four-round virtual onsite. The main thing that stood out was how little they cared about generic AI talk and how much they cared about low-level details like memory, compute, parallelism, GPU utilization, and whether I actually understood things under the hood. My LLM fundamentals round felt good because I could talk through transformers, position embeddings, BERT, and even a small agent design, but the system design round was the hardest by far because they pushed into distributed training at huge scale and wanted more hands-on tool knowledge. The coding rounds were a mix of common problems and performance-minded thinking, and I got the sense they prefer people who can build from scratch rather than hide behind libraries. Overall, it felt more technical and harder to surface-prep for than most other interview loops I’ve done."
Software Engineer · Senior / L5
xAI · a year ago
"I applied online and the process started with a 30-minute engineer screen instead of a recruiter screen. After that, they scheduled what I was told were the final rounds: two coding interviews on one day and a design round the next day. The first coding round was a standard LRU cache interview and felt fair, but the second one was rough because the interviewer gave me a big LLM-related codebase, refused to clarify inputs or outputs, and just kept telling me to read the code. That same evening, they canceled my last design round and then ghosted me when I asked for an explanation. Overall it felt very inconsistent: one interviewer was reasonable, the next was not, and the process ended without any real closure."
Software Engineer
T
Temenos · 2 years ago
"> My interview process with Temenos consisted of multiple rounds, including discussions on Core Java, Spring Boot, microservices, system design, databases, and my previous experience in banking and enterprise software projects. The interviewers were knowledgeable and focused on understanding both technical depth and problem-solving approach. What went well was the detailed discussion around real-world projects, architecture decisions, and domain experience. One challenging aspect was the depth of some technical and design questions, which required a strong understanding of system architecture and trade-offs. Overall, the process was professional, structured, and relevant to the role."
Software Engineer · Senior / L5
Robinhood · 2 years ago
"Went well, the interview process was pretty easy. If you do your research and study up you should have no problem"
Software Engineer · Entry Level / L3
Meta · 2 years ago
"Coding focussed with focus on leetcode style problems. The design prompts are simple and abstract like design netflix or design instagram, etc. It is upto you to drive design discussion and this round decides leveling"
Software Engineer · Senior / L5
Compass · 3 years ago
"Interviewers were very helpful throughout. They expect you to communicate your thought process throughout"
Software Engineer
Amazon · 3 years ago
"The interview was quite smooth. All of the interviewers asked their direct questions with no further small talk."
Software Engineer · Senior / L5
Microsoft · 3 years ago
"Intro and Most critically challanging past project (around 10-15 minutes) Design a configuration service which is responsible for updating configurations existing on n number of microservices existing in our architecture - High level System design discussion (arond 25-30 minutes)"
Software Engineer

Related courses

Course

AI Engineering Interview Prep

Ace the AI engineering interview at top companies like Google, Anthropic, Meta, and OpenAI. Learn from detailed frameworks, example questions, and mock interviews with senior engineers. Practice system design, AI/ML concepts, AI-assisted coding, and behavioral rounds.

Course

Generative AI Interviews

Ace your interviews at AI-first and AI-powered companies. Learn to explain how Gen AI really works, where it fails, and how to use it responsibly. Understand how top tech companies build and deploy AI products and how generative AI works in production, from LLMs and RAG systems to real-world deployment.