InterviewDryRun
Medium

Design an LRU Cache

Implement an LRU cache. Input: first line capacity, then commands: 'put k v' or 'get k'. Print the result of every get (-1 if missing). Both operations must be O(1).

Sample input
2
put 1 1
put 2 2
get 1
put 3 3
get 2
put 4 4
get 1
get 3
get 4
Expected output
1
-1
-1
3
4

Stuck? Read how to approach Design an LRU Cache: what the interviewer looks for, an answer outline and common mistakes.

OUTPUT

Press Run tests to check your code, or Run to try the input on the left. Everything runs in your browser.

Passing the tests is half the round

In an interview you also explain the approach, state the complexity and handle a follow-up while someone listens. Max asks this exact question, pushes on the weak spots and scores you at the end.

Practice with Max

Free: a 5-minute voice round and unlimited typed drills, with scored feedback. No card, and your camera is never recorded.

More practice problems