Reported November 2023
Navantree

Compare N-Ary Tree Leaf Sequences

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

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

Navan reported this one in November 2023, and the detail that matters is in the statement: each tree comes as parallel parent and values arrays, with the root marked by -1. You compare the leaf-value sequences of two N-ary trees, left to right, and the statement says to go iterative. With up to 200000 nodes, recursion will blow the stack. It's a tree problem dressed up as an array problem. If you blank on the traversal during the live OA, StealthCoder is the invisible safety net that reads the screen and hands you a working solution.

The problem

Each N-ary tree is represented by parallel parent and values arrays. The root has parent -1; every other entry names its parent index. Children are ordered by node index. Return whether the two trees have identical leaf-value sequences from left to right. Use an iterative traversal.

Function
sameNaryLeafValues(parent1: int[], values1: int[], parent2: int[], values2: int[]) → boolean

Examples
Example 1
parent1 = [-1,0,0]
values1 = [1,2,3]
parent2 = [-1,0,0]
values2 = [7,2,3]
return = true
Both leaf sequences are [2,3].
Example 2
parent1 = [-1,0,0]
values1 = [1,2,3]
parent2 = [-1,0,0]
values2 = [1,3,2]
return = false
Leaf order differs.

Constraints
Each tree has between 1 and 200000 nodes.
Each input is a valid rooted tree with exactly one -1 parent.
Node values fit in a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that you never need to build a real tree. Children are ordered by node index, so a node is a leaf exactly when its index never appears in the parent array. Scan parent1 once, mark every index that shows up as a parent, then walk indices 0 to n-1 in order and collect values for unmarked ones. Index order among leaves gives you the left-to-right order. Do the same for tree two and compare the lists. That's O(n) with no recursion at all. The pitfall is assuming leaf order needs a DFS and then overflowing the stack at 200000 nodes. Another miss is comparing node values instead of only leaf values, since Example 1 has different roots but still returns true. Careful: this index-order shortcut holds only if left-to-right order matches index order, which the statement says it does. If you freeze live, StealthCoder is the hedge that gets you the linear scan.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Compare N-Ary Tree Leaf Sequences 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Navan reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Compare N-Ary Tree Leaf Sequences FAQ

What's the trick to Compare N-Ary Tree Leaf Sequences?+

A leaf is any node whose index never appears in the parent array. Mark parents in one pass, then read values in index order for the unmarked nodes. Since children are ordered by index, that gives the left-to-right leaf sequence without building the tree.

Do I need an actual DFS for this?+

Not really. The statement asks for an iterative traversal, but the parent-array scan satisfies the intent and is simpler. If you prefer a traversal, use an explicit stack and push children in reverse index order. Both are O(n), but the scan has less code to get wrong.

How hard is this one really?+

Easy to medium. The logic is short once you spot the leaf definition. The difficulty is the input format and the size limit. At 200000 nodes, recursive DFS risks stack overflow, so an iterative approach is the safe choice.

What edge cases should I test?+

Test a single-node tree, where the root itself is the only leaf. Test trees with different leaf counts, which must return false right away. Also test the same leaf values in a different order, like Example 2, and trees with different internal values but equal leaves, like Example 1.

How do I prep for this in 48 hours?+

Practice reading parent-array trees and computing child counts or leaf flags in one pass. Write one iterative stack traversal from memory. Then run both examples by hand. That covers what this Navan problem from November 2023 actually tests.

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

OA at Navan?
Invisible during screen share
Get it