← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
May 2026

Summary

Amazon coding round for a Software Engineer position, two algorithm questions back to back. Pretty standard stuff if you've been grinding leetcode, but the pressure of doing it live is a different thing entirely.

Questions Asked (2)

Q1

Given the root of a binary tree and two distinct nodes p and q that both exist in the tree, find and return their lowest common ancestor. A node can be its own ancestor.

Algorithms & Data Structures
Author's notes

Classic tree problem and I still fumbled the base case for a second.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then explain a recursive DFS solution that returns the LCA by checking if p and q are found in the left and right subtrees. Emphasize the O(n) time and O(h) space complexity, and discuss iterative alternatives if needed.

Pro tip: Mention that the recursive solution assumes both nodes exist; if not guaranteed, you'd need to verify their presence first. Also, note that the problem can be solved iteratively using parent pointers for O(1) space if the tree nodes have parent references.

1. Clarify the problem

Confirm that p and q are distinct and both exist in the tree. Ask if the tree is binary (not BST) and if nodes have parent pointers. Clarify that a node can be its own ancestor.

2. Choose an approach

Decide between recursive DFS (simple, O(n) time, O(h) space) or iterative with parent pointers (O(n) time, O(1) space if parents exist). For most interviews, the recursive approach is expected.

3. Explain the recursive algorithm

Base case: if root is null or root is p or q, return root. Recursively search left and right subtrees. If both return non-null, root is the LCA; otherwise return the non-null result.

4. Analyze complexity

Time: O(n) worst-case, as each node is visited once. Space: O(h) for recursion stack, where h is tree height (O(n) worst-case for skewed tree, O(log n) for balanced).

5. Discuss edge cases and alternatives

Handle cases where p or q is the root, or one is ancestor of the other. Mention iterative solutions using parent pointers or path-to-root comparison if applicable.

Key Points to Mention

  • Definition of LCA: deepest node that has both p and q as descendants (a node can be its own descendant).
  • Recursive DFS approach: return root if it matches p or q; combine left and right results.
  • Time complexity O(n) and space complexity O(h) due to recursion stack.
  • Edge cases: p or q is root, one is ancestor of the other, skewed tree.
  • Alternative: iterative with parent pointers for O(1) space if nodes have parent references.
  • Assumption that both nodes exist; if not, need to verify presence first.

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

Q2

You're given an array where each element represents the money in a house. You can't rob two houses that are next to each other. What's the maximum amount you can collect?

Algorithms & Data Structures
Author's notes

This is the house robber dp problem, pretty much verbatim.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize this as the classic House Robber problem and solve it using dynamic programming. Define the state as the maximum amount robbed up to house i, then derive the recurrence: dp[i] = max(dp[i-1], dp[i-2] + nums[i]). Optimize space to O(1) by keeping only the last two values.

Pro tip: After presenting the optimal solution, briefly discuss edge cases (empty array, single house) and how you would test the solution. This shows attention to detail and production-ready thinking, which Amazon values.

1. Clarify the problem

Confirm that houses are in a line (not circular), that you cannot rob adjacent houses, and that all amounts are non-negative. Ask if the array can be empty or have one element.

2. Define the DP state

Let dp[i] be the maximum amount that can be robbed from the first i houses. Explain that at each house, you either skip it (take dp[i-1]) or rob it (take dp[i-2] + nums[i]).

3. Derive the recurrence

Write the recurrence relation: dp[i] = max(dp[i-1], dp[i-2] + nums[i]). Base cases: dp[0] = 0, dp[1] = nums[0].

4. Optimize space

Observe that only the last two DP values are needed, so replace the array with two variables (prev2 and prev1) to achieve O(1) space.

5. Analyze complexity and test

State time complexity O(n) and space O(1). Walk through a small example (e.g., [2,7,9,3,1]) to verify correctness and discuss edge cases.

Key Points to Mention

  • Dynamic programming approach with optimal substructure
  • Recurrence relation: dp[i] = max(dp[i-1], dp[i-2] + nums[i])
  • Base cases: dp[0] = 0, dp[1] = nums[0]
  • Space optimization from O(n) to O(1) using two variables
  • Time complexity O(n) and space complexity O(1)
  • Edge cases: empty array, single house, all houses same value

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