LRU Cache

Total Accepted: 44698 Total Submissions: 296097 My Submissions

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

Show Tags
Data Structure
Have you met this question in a real interview?
Yes
No
When did you meet this question?
1 week ago
1 month ago
3 months ago
6 months ago
More than 6 months ago
How long have you been interviewing?
Not yet started
1 month
3 months
6 months
More than 6 months
Which company asked you this question?
Adobe
Airbnb
Alation
Alibaba
Amazon
Apple
Arista
Baidu
Bloomberg
Box
Bungie
Cisco
CreditEase
Deutsche Bank
Dropbox
eBay
Electronic Arts
Epic Systems
Expedia
Facebook
Fortinet
Goldman Sachs
Google
GrabTaxi
Intel
Jane Street
JPMorgan
Jump Trading
LinkedIn
Marvel
Matlab
McKesson
Microsoft
Nvidia
Oracle
Pinterest
Pocket Gems
Qualtrics
Rackspace
Sina
Snapchat
Sumologic
Tencent
Tradeshift
Twitter
Two Sigma
Uber
Yahoo!
Yandex
Yelp
Zenefits
Zynga

Discuss


You have not signed in, cannot submit your code.
Submit failed, try again please.

Submission Result: {[{ statusText }]}More Details

Share your acceptance!

Runtime Error Message: {[{ runtimeError }]}
Last executed input: {[{ lastExecutedTestCase }]}
Stdout: {[{ resultStdOutput }]}
{[{ resultCodeOutput }]}
Input: {[{ resultWaTestCaseInput }]}
Output: {[{ resultWaTestCaseOutput }]}
Expected: {[{ resultWaTestCaseExpected }]}
Stdout: {[{ resultStdOutput }]}

Send Feedback