Minimum distance between two nodes in an N-ary tree
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Snowflake's two coding screens round and at Scale AI and Google, last asked Feb 2026. It tests trees, lowest common ancestor and dfs.
You are given the root of an N-ary tree. Every node has an integer val, unique within the tree, and a list children of its direct children. There are no parent pointers.
Implement:
def min_distance(root: Node | None, u: int, v: int) -> int
Return the number of edges on the path between the node whose value is u and the node whose value is v. If either value is not in the tree, return -1. If u == v and the value exists, return 0.
Constraints
- 0 <= number of nodes <= 10^5
- The tree can be very deep (for example, a single chain), so do not rely on deep recursion.
- One node may be an ancestor of the other.
Example 1
10
/ | \
20 30 40
/ \ |
50 60 70
|
80
-
min_distance(root, 50, 80)returns3: the path is 50 → 20 → 60 → 80. -
min_distance(root, 80, 70)returns5: the path goes up to 10 and down to 70.
Example 2
Same tree. min_distance(root, 20, 80) returns 2, because 20 is an ancestor of 80. min_distance(root, 30, 99) returns -1, because 99 is not in the tree.
Be ready to discuss how you would answer many queries on the same tree quickly.
Clarifying questions
- Can
rootbeNone, and should that return-1for any query? - Are the values guaranteed unique, so each value names one node?
- Will there be one query or many on the same tree?
- How deep can the tree be: should the solution avoid recursion entirely?
What a strong answer covers
- One iterative pass (BFS or an explicit-stack DFS) that records each node's parent and depth, then finds the lowest common ancestor by lifting the deeper node to the same depth and walking both up together. The distance is
depth[u] + depth[v] - 2 * depth[lca]. That is O(n) time and O(n) space. - Alternatively, records the root-to-node paths of
uandvand counts the shared prefix. - No recursion, so a chain of 10^5 nodes does not overflow the stack.
- Edge cases: an empty tree,
uorvmissing,u == v, one node an ancestor of the other, and the root as one of the nodes. - Tests: the example tree's queries, a single-chain tree, a star-shaped tree, and a random tree checked against a BFS from
utov.
Follow-ups
- Answer many queries quickly: precompute depths and use binary lifting (O(log n) per query) or an Euler tour with a sparse table (O(1) per query).
- Return the path itself, not just its length.
- The tree is given as an edge list, not as nodes with children.
- Edges have weights.
Where it's asked
- Snowflake · Two coding screens · Last asked Feb 202660 min · Two back-to-back live coding rounds, often on the same day; some loops pair coding with system design · Varies: some screens forbid AI, some 2026 screens allow it. The scheduling email says.
- Scale AI
What it tests
trees · lowest common ancestor · dfs · binary lifting
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.


