← Amazon Interview Insights

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

Intermediate
May 2026

Summary

Amazon SWE online assessment with a tree problem that looks straightforward until you actually sit down to code it. Nothing too wild but the edge cases are sneaky.

Questions Asked (1)

Q1

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

Algorithms & Data Structures
Author's notes

My first instinct was BFS and track depth plus parent for each node, which works fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a single traversal (BFS or DFS) to track each node's parent and depth. Then compare the parent and depth of x and y to classify their relationship.

Pro tip: Clarify edge cases upfront, such as x and y being the same node or not present in the tree, and discuss how to handle them. This shows attention to detail and robustness.

1. Clarify definitions and edge cases

Confirm what 'siblings' and 'cousins' mean: siblings share the same parent; cousins have the same depth but different parents. Ask about edge cases like x == y, missing nodes, or duplicate values.

2. Choose traversal strategy

Decide between BFS (level-order) or DFS (pre-order) to record parent and depth for each node. BFS naturally processes nodes level by level, making depth tracking straightforward.

3. Traverse and record metadata

During traversal, maintain a map or variables to store the parent and depth of x and y. For BFS, enqueue children with their parent and depth; for DFS, pass parent and depth as parameters.

4. Compare and classify

After traversal, compare the recorded parent and depth: if parents are equal, they are siblings; else if depths are equal, they are cousins; otherwise, neither.

5. Analyze complexity and optimize

State time and space complexity: O(n) time and O(n) space for the map, or O(h) space for DFS recursion. Discuss early termination if both nodes are found.

Key Points to Mention

  • Definition of siblings and cousins in a binary tree
  • BFS vs DFS trade-offs for tracking parent and depth
  • Handling edge cases: x == y, nodes not present, duplicate values
  • Time and space complexity analysis (O(n) time, O(n) or O(h) space)
  • Early termination when both nodes are found
  • Avoiding multiple traversals by recording metadata in one pass

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