← Amazon Interview Insights

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

IntermediatePrefer not to say
May 2026

Summary

Amazon SWE coding round, two algorithmic problems back to back. The second one was a tree relationship classifier that started simple and got messier as the interview went on. Came away feeling okay about it but not great.

Questions Asked (2)

Q1

Given an array of integers, find the length of the longest subsequence where any two adjacent elements differ by at most k (a generalization of the classic longest consecutive sequence problem).

Algorithms & Data Structures
Author's notes

I knew LC 128 cold so my first instinct was to just sort and scan, which works fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (e.g., subsequence vs. subarray, whether order matters, and if k is non-negative) before proposing a solution. Then, discuss a dynamic programming approach where you sort the array and use a data structure to efficiently find the longest valid subsequence ending at each element. Analyze time and space complexity, and consider edge cases.

Pro tip: Mention that sorting the array simplifies the problem because any valid subsequence can be rearranged into sorted order without affecting the adjacency condition. This insight often leads to a more efficient solution and shows you can reduce problem complexity.

1. Clarify the problem

Ask questions to confirm details: Is the subsequence required to maintain original order? Can k be negative? Are there duplicate values? What should be returned if the array is empty?

2. Explore brute force and identify inefficiencies

Discuss a naive O(2^n) approach to generate all subsequences, then explain why it's impractical and motivate the need for a more efficient algorithm.

3. Propose an optimized approach

Sort the array and use dynamic programming with a balanced BST or segment tree to find the longest subsequence ending at each element, considering previous elements within k.

4. Analyze complexity and edge cases

State the time complexity (e.g., O(n log n)) and space complexity, and walk through edge cases like empty array, single element, or all elements within k.

5. Test with examples

Run through a small example to verify the logic, such as array [1, 3, 5, 7] with k=2, and show how the DP state updates.

Key Points to Mention

  • Sorting the array transforms the problem into finding the longest subsequence where adjacent elements differ by at most k, which can be solved with DP.
  • Dynamic programming state: dp[i] = length of longest valid subsequence ending at index i.
  • Use a balanced BST or segment tree to efficiently query the maximum dp value among elements in the range [arr[i]-k, arr[i]+k].
  • Time complexity: O(n log n) due to sorting and O(log n) queries per element.
  • Space complexity: O(n) for the DP array and data structure.
  • Edge cases: empty array, k=0 (requires equal elements), negative k (invalid), and large input sizes.

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

Q2

Given a tree and two node references, determine whether the two nodes are siblings (same parent), cousins (same depth but different parent), or neither.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The prompt left tree arity open, which I did not catch until I was halfway through writing a binary-tree solution.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the tree structure and node references, then propose a solution that finds the parent and depth of each node. Compare parents and depths to classify the relationship, discussing time/space trade-offs and edge cases.

Pro tip: Mention that you can optimize by stopping early if you find both nodes during a single traversal, and always handle edge cases like the same node or root nodes.

1. Clarify assumptions

Ask about the tree type (binary, n-ary), whether parent pointers exist, and if node references are guaranteed to be in the tree. Confirm the definitions of siblings and cousins.

2. Choose traversal strategy

Decide between BFS (to track depth and parent level by level) or DFS (to find parents and depths recursively). Consider iterative vs recursive based on constraints.

3. Implement relationship check

During traversal, record each node's parent and depth. After traversal, compare: if parents equal and depths equal -> siblings; if depths equal but parents differ -> cousins; else neither.

4. Analyze complexity and trade-offs

State time complexity O(n) and space complexity O(n) for BFS/DFS. Discuss optimizations like early termination or using parent pointers if available.

5. Test edge cases

Consider cases: same node, root and child, nodes at different depths, one node is ancestor of the other, and nodes not in tree. Explain how your solution handles them.

Key Points to Mention

  • Definition of siblings: same parent; cousins: same depth but different parents.
  • BFS naturally tracks depth and parent, making it suitable for level-order comparison.
  • DFS can also work by passing depth and parent during recursion.
  • Time complexity O(n) and space complexity O(n) for both approaches.
  • Edge cases: same node, root nodes, nodes at different depths, and nodes not present.
  • Optimization: early exit if both nodes found, or use parent pointers if available.

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