FastPrepTree Node Relationship
Problems/Tree Node Relationship

Tree Node Relationship

Amazon logoAmazon● EasyFULLTIMENEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given an undirected tree with n nodes, numbered from 0 to n - 1. The tree is rooted at node 0. Each entry of edges joins two nodes. You also get two queried nodes, nodeA and nodeB. Decide how they are related.

The depth of a node is its distance from the root, counted in edges. Return "siblings" if the two nodes have the same parent. Return "cousins" if they are at the same depth but have different parents. Otherwise, return "others".

Function

classifyTreeNodeRelationship(n: int, edges: int[][], nodeA: int, nodeB: int) → String

Examples

Example 1

n = 7edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]nodeA = 3nodeB = 4return = "siblings"

Nodes 3 and 4 both have parent 1. They share a parent, so the answer is "siblings".

Example 2

n = 7edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]nodeA = 3nodeB = 5return = "cousins"

Nodes 3 and 5 are both at depth 2, but their parents are different. So the answer is "cousins".

Example 3

n = 7edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]nodeA = 1nodeB = 6return = "others"

The two nodes are at different depths. The first is a child of the root, and the second is one level deeper. They also have different parents, so the answer is "others".

Constraints

  • The input graph is a valid tree rooted at node 0. Both queried nodes are valid node ids.

More Amazon Phone Screen questions

  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
  • Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
  • Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
  • Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Symmetric Binary TreeDocuSign · PHONE SCREENDocuSign interview questions
  • Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
  • Binary Tree Level-Order Traversal by LevelsAdobe · PHONE SCREEN · ONSITE INTERVIEWAdobe interview questions
  • Level of a Node in a Binary TreeBloomberg LP · PHONE SCREENBloomberg LP interview questions
  • Left View of a Binary TreeMygate · PHONE SCREENMygate interview questions
  • Invert a Binary TreeCapital One · PHONE SCREENCapital One interview questions
  • Maximum-Scoring Consecutive Tree LevelsJane Street · PHONE SCREENJane Street interview questions
  • Minimum Edits to a Perfect Binary TreeSnowflake · PHONE SCREENSnowflake interview questions
  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
Saved
class Solution {
    public String classifyTreeNodeRelationship(int n, int[][] edges, int nodeA, int nodeB) {
        // write your code here
    }

}
Checking account…