Quick Overview

Implement an LRU cache with constant-time lookup, recency updates, and eviction, including existing-key updates, zero capacity, and concurrency pitfalls.

Implement an LRU Cache

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Implement a cache of fixed capacity with `get(key)` and `put(key, value)`. A successful get and any insertion or update make that key most recently used. When inserting a new key into a full cache, evict the least recently used key. Return `-1` for a missing key. ### Constraints & Assumptions Use the standard LRU formulation. Capacity is nonnegative; a zero-capacity cache stores nothing. Target average O(1) time per operation. ### Clarifying Questions to Ask - Does updating an existing key refresh its recency? - Can a stored value be `-1`, and would a distinct missing-value result be preferable in a production interface? ```hint Two forms of access You need both direct key lookup and constant-time movement within a recency order. ``` ### What a Strong Answer Covers - A hash index paired with a mutable recency order. - Existing-key updates, hit promotion, and eviction of the correct endpoint. - Consistent removal from both structures and behavior at capacity zero. ### Follow-up Questions - How would concurrent gets and puts preserve a single recency order? - How would time-based expiration interact with least-recently-used eviction?

Overview: Implement an LRU cache with constant-time lookup, recency updates, and eviction, including existing-key updates, zero capacity, and concurrency pitfalls.

Read the full Amazon Software Engineer interview experience this question came from

Implement a least recently used (LRU) cache with a fixed `capacity`. Given an ordered list of `operations`, start with an empty cache and process each operation in order. - `[0, key]` performs `get(key)`. Return the stored value and mark the key most recently used if it is present. Return `-1` for a missing key and leave the cache unchanged. A present key with value `-1` still counts as a hit and becomes most recently used. - `[1, key, value]` performs `put(key, value)`. Insert or update the key and mark it most recently used. When inserting a new key into a full cache, evict the least recently used key. A cache with capacity zero never stores anything. Return the results of the `get` operations in the order they occur. Each `put` contributes no result.

Constraints

  • 0 <= capacity <= 2^53 - 1.
  • 0 <= operations.length <= 2^31 - 1.
  • Each record is exactly [0, key] or [1, key, value].
  • Keys and stored values are integers from -(2^53 - 1) to 2^53 - 1, inclusive.
  • Target O(1) average time per get or put.

Examples

Input: (2, [[1, 10, 100], [1, 20, 200], [0, 10], [1, 30, 300], [0, 20], [0, 30], [0, 10]])

Expected Output: [100, -1, 300, 100]

Explanation: Getting key 10 makes it most recently used. Putting key 30 then evicts key 20.

Input: (2, [[1, 4, 40], [1, 5, 50], [1, 4, 41], [1, 6, 60], [0, 4], [0, 5], [0, 6]])

Expected Output: [41, -1, 60]

Explanation: Updating key 4 changes its value to 41 and makes it most recently used. Putting key 6 evicts key 5.

Hints

  1. Trace the keys from least to most recently used after a hit, a miss, and an update.

Implement an LRU Cache

mediumAmazonSoftware Engineer

Implement a least recently used (LRU) cache with a fixed capacity. Given an ordered list of operations, start with an empty cache and process each operation in order.

  • [0, key] performs get(key). Return the stored value and mark the key most recently used if it is present. Return -1 for a missing key and leave the cache unchanged. A present key with value -1 still counts as a hit and becomes most recently used.
  • [1, key, value] performs put(key, value). Insert or update the key and mark it most recently used. When inserting a new key into a full cache, evict the least recently used key. A cache with capacity zero never stores anything.

Return the results of the get operations in the order they occur. Each put contributes no result.

Examples

Example 1
Input
(2, [[1, 10, 100], [1, 20, 200], [0, 10], [1, 30, 300], [0, 20], [0, 30], [0, 10]])
Output
[100, -1, 300, 100]
Notes
Getting key 10 makes it most recently used. Putting key 30 then evicts key 20.
Example 2
Input
(2, [[1, 4, 40], [1, 5, 50], [1, 4, 41], [1, 6, 60], [0, 4], [0, 5], [0, 6]])
Output
[41, -1, 60]
Notes
Updating key 4 changes its value to 41 and makes it most recently used. Putting key 6 evicts key 5.

Constraints

  • •0 <= capacity <= 2^53 - 1.
  • •0 <= operations.length <= 2^31 - 1.
  • •Each record is exactly [0, key] or [1, key, value].
  • •Keys and stored values are integers from -(2^53 - 1) to 2^53 - 1, inclusive.
  • •Target O(1) average time per get or put.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def simulate_lru_cache(capacity: int, operations: list[list[int]]) -> list[int]:
Sample call
simulate_lru_cache(2, [[1, 10, 100], [1, 20, 200], [0, 10], [1, 30, 300], [0, 20], [0, 30], [0, 10]])
Language
Case 1
Input
(2, [[1, 10, 100], [1, 20, 200], [0, 10], [1, 30, 300], [0, 20], [0, 30], [0, 10]])
Expected
[100, -1, 300, 100]
Case 2
Input
(2, [[1, 4, 40], [1, 5, 50], [1, 4, 41], [1, 6, 60], [0, 4], [0, 5], [0, 6]])
Expected
[41, -1, 60]

Browse More Questions

Show the approach

Approach

Use a hash index for key lookup and a doubly linked order for recency. The Java and Python references use ordered maps that maintain the same relationship. The JavaScript and C++ references keep the index and list explicitly.

After each operation, the index contains exactly the resident keys, and the order runs from the least recent entry to the most recent one. A hit finds the entry in the index and moves it to the most recent end. A miss leaves both structures untouched. A put for a resident changes its value and promotes it without changing membership. A put for a new key adds it at the most recent end. If the cache was full, the previous least recent entry is removed from both the order and the index. At capacity zero, every put is ignored. These transitions preserve the invariant and ensure that each returned value reflects its request's position in the trace.

The hash index detects presence independently of the stored value, so a stored -1 refreshes recency even though its returned value equals that of a miss.

Time complexity:
O(n) on average for n operations, or O(1) average per operation.
Space complexity:
O(1 + min(capacity, n)) auxiliary space, plus O(g) for g get results.