← Amazon Interview Insights

Amazon·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
Jun 2026

Summary

Amazon software engineer coding round with two back-to-back problems. Nothing too wild but the LRU cache had more moving parts than I expected and I spent way too long second-guessing my doubly linked list logic.

Questions Asked (2)

Q1

Design and implement an LRU cache with O(1) get and put operations, including eviction of the least recently used item when capacity is exceeded.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew the answer involved a hashmap plus a doubly linked list but explaining why out loud while also coding it was harder than I anticipated.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying requirements and constraints, then propose a hash map combined with a doubly linked list to achieve O(1) operations. Walk through the design, implement the core methods, and discuss trade-offs and edge cases.

Pro tip: Mention that you would use a doubly linked list with sentinel nodes to simplify edge cases, and that you would consider thread-safety if the cache is shared across threads.

1. Clarify requirements and constraints

Ask about capacity limits, thread-safety, and whether keys/values are generic. Confirm that get and put must be O(1) and that eviction is based on least recent use.

2. Propose data structures

Explain that a hash map provides O(1) access to nodes, and a doubly linked list maintains recency order. Together they enable O(1) get, put, and eviction.

3. Design the algorithm

Detail how get moves the accessed node to the front (most recently used), and put inserts or updates a node, moving it to the front and evicting the tail if capacity is exceeded.

4. Implement the code

Write clean code for the LRU cache class, using sentinel head and tail nodes to avoid null checks. Include helper methods for adding and removing nodes.

5. Analyze complexity and edge cases

State that all operations are O(1) time and O(capacity) space. Discuss edge cases like capacity 0 or 1, updating existing keys, and thread-safety if needed.

Key Points to Mention

  • Hash map for O(1) key lookup, mapping keys to nodes in the linked list.
  • Doubly linked list to maintain recency order, with most recently used at the head and least recently used at the tail.
  • Sentinel nodes (dummy head and tail) to simplify insertion and removal logic.
  • Eviction policy: when capacity is exceeded, remove the node at the tail (least recently used).
  • Time complexity: O(1) for both get and put; space complexity: O(capacity).
  • Thread-safety considerations: use locks or concurrent data structures if the cache is shared across threads.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Rotate an n x n matrix 90 degrees clockwise in place without allocating a second matrix.

Algorithms & Data Structures
Author's notes

Transpose then reverse each row.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the in-place constraint and confirm matrix dimensions, then present the layer-by-layer rotation approach. Explain the four-way swap for each element in a layer, and analyze time and space complexity.

Pro tip: Mention that you can also achieve the rotation by first transposing the matrix and then reversing each row, which is simpler to implement and less error-prone, but still in-place.

1. Clarify requirements and constraints

Confirm that the matrix is n x n, rotation is 90 degrees clockwise, and no additional matrix can be allocated. Ask if the matrix can be modified in place.

2. Choose an approach

Decide between layer-by-layer four-way swapping or transpose-then-reverse. Both are O(n^2) time and O(1) space; pick the one you can explain most clearly.

3. Explain the algorithm

For layer-by-layer: iterate over each layer from outermost to innermost, and for each element in the top row of the layer, perform a four-way swap with corresponding elements on the right, bottom, and left sides.

4. Analyze complexity and edge cases

State that time complexity is O(n^2) since each element is visited once, and space complexity is O(1). Mention edge cases like n=0 or n=1.

5. Test with a small example

Walk through a 3x3 or 4x4 matrix to verify the rotation, ensuring indices are correct and no element is overwritten prematurely.

Key Points to Mention

  • In-place rotation means O(1) extra space, not O(n^2).
  • Layer-by-layer approach: process each concentric layer, swapping four elements at a time.
  • Index mapping: for element at (i, j) in layer, swap with (j, n-1-i), (n-1-i, n-1-j), and (n-1-j, i).
  • Alternative: transpose the matrix (swap a[i][j] with a[j][i]) then reverse each row.
  • Time complexity: O(n^2) because each cell is touched a constant number of times.
  • Edge cases: n=0 (empty matrix), n=1 (no rotation needed).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.