
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!
Add answer anonymously...
Interview Questions from Popular Companies

3.7
• 8.8k Interviews

4.0
• 5.4k Interviews

3.5
• 4.2k Interviews

3.6
• 8k Interviews

3.7
• 6k Interviews

3.7
• 6.2k Interviews

3.5
• 4.2k Interviews

3.7
• 5.1k Interviews
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

