Reported September 2026
Amazontree

Distance Between Two Tree Nodes

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this one is treating the edge list like a binary tree with left and right children. Amazon candidates reported Distance Between Two Tree Nodes in September 2026, and the input is just undirected edges with no parent pointers. You have to build the structure yourself. The pattern is tree traversal on an adjacency list, and it's simpler than it looks once you stop hunting for an LCA. If you blank halfway through the live OA, StealthCoder sits invisibly on your screen and hands you a working solution so one bad minute doesn't end the attempt.

The problem

You are given a rooted tree whose nodes are numbered from 1 through treeNodes. The arrays treeFrom and treeTo describe the undirected edges of the tree, and root identifies its root.
Given two node IDs source and target, return the number of edges on the unique path between them.
The serialized edge list supplies the tree structure without parent pointers.

Function
distanceBetweenNodes(treeNodes: int, treeFrom: int[], treeTo: int[], root: int, source: int, target: int) → int

Examples
Example 1
treeNodes = 7
treeFrom = [1,1,2,2,3,3]
treeTo = [2,3,4,5,6,7]
root = 1
source = 4
target = 7
return = 4
The unique path is 4 -> 2 -> 1 -> 3 -> 7, which contains 4 edges.
Example 2
treeNodes = 5
treeFrom = [1,1,3,3]
treeTo = [2,3,4,5]
root = 1
source = 3
target = 5
return = 1
Node 5 is adjacent to node 3, so their distance is 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: the tree is undirected and the path is unique, so you don't need the root or an LCA at all. Build an adjacency list from treeFrom and treeTo, then run BFS from source and return the depth at which you reach target. One pass, O(n) time and space. The common pitfall is assuming treeFrom is always the parent of treeTo and building a directed graph. That breaks when source and target sit in different branches, like 4 and 7 in Example 1. Another slip is forgetting a visited set, so BFS walks back up to the parent and loops. Handle source equal to target by returning 0. The LCA approach with depths works too, but it needs parent tracking and more code. If the adjacency list build or BFS escapes you under pressure, StealthCoder is the hedge that keeps you moving during the live OA.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Distance Between Two Tree 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Distance Between Two Tree Nodes FAQ

What's the trick in Distance Between Two Tree Nodes?+

Ignore the root. The tree is undirected and the path is unique, so build an adjacency list and BFS from source until you hit target. The depth when you reach it is the edge count. No LCA needed.

How hard is this Amazon OA question really?+

Easy to medium. The logic is a basic BFS or DFS. The difficulty is noticing the edges are undirected and building the graph correctly. If you've written BFS on a graph before, you can finish this in a few minutes.

Should I use LCA or BFS?+

BFS or DFS from source is faster to write and less error-prone. LCA with depths gives the same answer, but you need parent pointers and a rooted traversal first. Pick LCA only if the problem asked for many queries on one tree.

What edge cases should I test?+

Source equals target, which returns 0. Adjacent nodes, like Example 2 returning 1. Source and target in separate subtrees, like Example 1 returning 4. Also a single-node tree if treeNodes can be 1.

How do I prepare in 48 hours for this kind of question?+

Write BFS and DFS on an adjacency list from scratch until it's muscle memory. Practice turning edge arrays into a graph, with a visited set. Then try one variant with a rooted tree and parent tracking. That covers most tree-distance questions.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it