Shortest Path Between BST Nodes
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google reported this one in October 2026, and the input is a BST, which is the whole hint. Nobody wants you building a graph and running BFS from one node to the other. The tree ordering already tells you where each value lives, so brute force over every node pair is the wrong instinct. This is lowest common ancestor in disguise: find the split point, count edges down to each target, add them. Two edge cases decide whether you pass: a missing value returns -1, and equal targets that exist return 0. If you blank on the walk, StealthCoder is the invisible safety net during the live OA.
The problem
You are given the root of a binary search tree with distinct integer values and two target values, first and second. Return the number of edges on the unique shortest path between their nodes. If either target value does not occur in the tree, return -1. If first == second and that value exists, return 0. Use the binary-search-tree ordering to locate where the two search paths split, then measure the distance from that split node to each target. Examples Example 1 root = [6,2,8,0,4,7,9,null,null,3,5] first = 2 second = 8 return = 2 The shortest path is 2 -> 6 -> 8, which contains two edges.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Start at the root. While both targets are smaller than the current node, go left. While both are larger, go right. The first node where they diverge, or where one equals the current node, is the split node, which is the LCA. Then walk from the split to each target using normal BST search, counting edges. Sum the two counts. If either search falls off the tree, return -1. That's O(h) time and O(1) space iteratively. The common pitfall is finding the LCA first without checking that both values exist. Say first = 2 and second = 100 where 100 is absent: the LCA walk happily returns a node, and you report a bogus distance. Do the existence check during the downward walks. Another miss is treating first == second as special when the general code already returns 0. If the logic slips under pressure, StealthCoder can cover you during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Shortest Path Between BST Nodes cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Path Between BST Nodes FAQ
What's the trick for Shortest Path Between BST Nodes?+
It's lowest common ancestor on a BST. Walk down from the root, going left if both targets are smaller and right if both are larger. The first node where they split is the LCA. Distance is the depth from the LCA to each target, added together.
How hard is this really?+
Easy to medium. The algorithm is short once you see the LCA link. The difficulty is in the edge cases: a missing target returns -1, and identical existing targets return 0. Most failed attempts miss the existence check, not the traversal.
Do I need BFS or a graph conversion?+
No. Converting to an adjacency list and running BFS works, but it ignores the BST ordering and costs O(n) time and memory. The ordering gives you O(h) with constant space, which is the version this problem is nudging you toward.
How do I handle a target that isn't in the tree?+
Verify it during the downward search from the split node. If the search hits a null pointer before finding the value, return -1 immediately. Don't trust the LCA walk alone, since it will return a node even when one value is absent.
How do I prepare in 48 hours for this Google OA?+
Write the iterative LCA on a BST from memory, then add distance counting and the -1 check. Test with equal targets, one target as the ancestor of the other, a missing value, and a single-node tree. That covers nearly every failure case.