FastPrepClassify Two Nodes as Siblings, Cousins, or Others
Problems/Classify Two Nodes as Siblings, Cousins, or Others

Classify Two Nodes as Siblings, Cousins, or Others

Amazon logoAmazon● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Amazon Experience 24 asks you to say whether two tree nodes are siblings, cousins, or neither. It was an onsite interview. The report does not say how the tree is written or how to treat repeated values. Here, the tree has unique values and the answers are lowercase labels. Our version is about 94% the same as the reported question.

A binary tree contains unique integer values. Given the values first and second of two distinct nodes, return:

  • "siblings" if they have the same parent.
  • "cousins" if they have the same depth but different parents.
  • "others" otherwise.

Function

classifyNodeRelationship(root: TreeNode, first: int, second: int) → String

Examples

Example 1

root = [1,2,3,4,5,6,7]first = 4second = 5return = "siblings"
root
1245367

Nodes 4 and 5 share parent 2.

Example 2

root = [1,2,3,4,5,6,7]first = 4second = 6return = "cousins"
root
1245367

Nodes 4 and 6 have equal depth and different parents.

Example 3

root = [1,2,3,4,null,null,7,8]first = 3second = 8return = "others"
root
124837

One node is at depth one and the other is at depth three.

Constraints

  • The tree contains between 2 and 100000 nodes.
  • Node values are unique and between -10^9 and 10^9.
  • Both target values occur in the tree and are distinct.

More Amazon Onsite Interview questions

  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dungeon GameONSITE INTERVIEW · Seen Nov 2026
  • Integer BreakONSITE INTERVIEW · Seen Nov 2026
  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Bottom View of a Binary TreeOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
  • Binary Tree Vertical Order TraversalBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Binary Tree Vertical Order with Stable TiesMeta · ONSITE INTERVIEWMeta interview questions
  • Top View of a Binary TreeInMobi · ONSITE INTERVIEWInMobi interview questions
  • Count LevelsBlackRock · OABlackRock interview questions
  • Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
  • Everyone Under a ManagerAdobe · PHONE SCREENAdobe interview questions
  • Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions
  • Path Between Two Binary-Tree NodesSnowflake · PHONE SCREENSnowflake interview questions

Interview reports that mention this question

  • Amazon New Grad SDE Onsite Interview Questions: 17 Experiences

Amazon interview experiences

  • Amazon SWE OA with AI Coding: 1 Experience
  • Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
  • Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
  • Amazon BIE Intern SQL and Interview Questions: 17 Experiences
  • Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
See Amazon hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public String classifyNodeRelationship(TreeNode root, int first, int second) {
      // write your code here
    }
}
Checking account…