Oracle Logo

Asked in Oracle and 21 others

<h3>LRU Cache Design Problem Statement</h3> <p>Design a data structure that implements a Least Recently Used (LRU) cache. The cache should support the following operations: <code>get(key)</code> and <code>put(key, value)</code>.</p> <p>When the cache reaches its capacity, it should invalidate the least recently used item before inserting a new item.</p> <h4>Follow these constraints:</h4> <ul> <li><code>get</code> should return the value of the key if the key exists in the cache. Otherwise, it should return -1.</li> <li><code>put</code> should update the value of the key if the key exists. Otherwise, it should insert the key-value pair. When the cache reaches its capacity, it should invalidate the least recently used item before inserting a new item.</li> </ul> <h4>Example:</h4> <pre> <strong>LRUCache cache = new LRUCache(2);</strong> <br /> cache.put(1, 1); <br /> cache.put(2, 2); <br /> cache.get(1); // returns 1 <br /> cache.put(3, 3); // evicts key 2 <br /> cache.get(2); // returns -1 (not found) <br /> cache.put(4, 4); // evicts key 1 <br /> cache.get(1); // returns -1 (not found) <br /> cache.get(3); // returns 3 <br /> cache.get(4); // returns 4 </pre></p>

AnswerBot
2mo

Implementing an LRU Cache involves using a data structure to store key-value pairs with a limit on size, evicting the least recently used.

  • Use a combination of a hash map and a doubly linked list for e...read more

Help your peers!
Select
Add answer anonymously...

Interview Questions from Popular Companies

Accenture Logo
3.7
 • 8.8k Interviews
Amazon Logo
4.0
 • 5.4k Interviews
HCLTech Logo
3.5
 • 4.2k Interviews
Infosys Logo
3.6
 • 8k Interviews
Cognizant Logo
3.7
 • 6k Interviews
Wipro Logo
3.7
 • 6.2k Interviews
Tech Mahindra Logo
3.5
 • 4.2k Interviews
Capgemini Logo
3.7
 • 5.1k Interviews
Senior Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits