FastPrepDistance Between Two Nodes in a Binary Tree
Problems/Distance Between Two Nodes in a Binary Tree

Distance Between Two Nodes in a Binary Tree

Bloomberg LP logoBloomberg LP● MediumINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A candidate's report on a Bloomberg internship interview lists this task as one of two coding questions in its only technical interview, an hour that also held a short behavioral part. It names the task in one line, with no example, limits or rules.

Our choices: the two nodes are given by their values, every value in the tree is different, both values are in the tree, and the distance counts edges, so it is 0 when both values name the same node. A tree has 1 to 500 nodes with values from 0 to 1,000,000,000.

The judged task matches the report at about 85%.

You are given the root of a binary tree and two integers p and q. Every node has a different value, and both p and q are values of nodes in the tree.

The distance between two nodes is the number of edges on the path between them. An edge is the link between a node and one of its children. The path may go up toward the root and then down again.

Return the distance between the node with value p and the node with value q. If p == q, they are the same node and the distance is 0.

Function

findDistance(root: TreeNode, p: int, q: int) → int

Examples

Example 1

root = [8,3,11,6,2,null,4,null,null,9,15]p = 6q = 15return = 3
root
8362915114

The path from 6 to 15 is 6 - 3 - 2 - 15. It goes up from 6 to 3, then down through 2 to 15. That is 3 edges.

The path does not use the root 8, because 3 is the lowest node that has both 6 and 15 below it.

Example 2

root = [8,3,11,6,2,null,4,null,null,9,15]p = 9q = 4return = 5
root
8362915114

The path is 9 - 2 - 3 - 8 - 11 - 4: up three edges to the root 8, then down two edges to 4. That is 5 edges.

Example 3

root = [5,1,null,7,null,12]p = 12q = 5return = 3
root
51712

Node 5 is above node 12, so the path only goes up: 12 - 7 - 1 - 5, which is 3 edges.

Example 4

root = [20,10,30]p = 30q = 30return = 0
root
201030

Both values name the same node, so the distance is 0.

Constraints

  • The tree has between 1 and 500 nodes.
  • 0 <= Node.val <= 1000000000
  • All node values are different.
  • p and q are both values of nodes in the tree.

More Bloomberg LP Phone Screen questions

  • N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
  • Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
  • Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
  • Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
  • Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake interview questions
  • Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
  • Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
  • Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
  • Transform and Prune a Mode-Valued Binary TreeGoogle · PHONE SCREENGoogle interview questions
  • Time to Burn a Binary Tree From One NodeAdobe · PHONE SCREENAdobe interview questions
  • Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions
  • Symmetric Binary TreeDocuSign · PHONE SCREENDocuSign interview questions

Bloomberg LP interview experiences

  • Bloomberg SWE Phone Screen and Onsite: 1 Experience
  • Bloomberg SWE Phone Screens with Two Teams: 1 Experience
  • Bloomberg AI Interview, Hiring Level Not Reported: 2 Experiences
  • Bloomberg Experienced SWE AI Coding Interviews: 2 Experiences
  • Bloomberg New Grad AI-Assisted Coding Rounds: 3 Experiences
  • Bloomberg New Grad SWE AI-Assisted Coding Rounds: 1 Experience
All Bloomberg LP interview questionsSee Bloomberg LP 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 int findDistance(TreeNode root, int p, int q) {
        // Write your code here.
    }
}
Checking account…