FastPrepCompare N-Ary Tree Leaf Sequences
Problems/Compare N-Ary Tree Leaf Sequences

Compare N-Ary Tree Leaf Sequences

Navan logoNavan● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Navan onsite interview for a full-time role. Our version follows the reported exercise. The report leaves out some input limits and interface details, and we filled those in.

Our version is about 95% the same as the reported question.

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.

More Navan interview questions

  • Time Based Key-Value StoreONSITE INTERVIEW · Seen Dec 2025
  • Type-Ahead Prefix MatchesONSITE INTERVIEW · Seen Dec 2024
  • Minimum Steps on a Circular Character DialONSITE INTERVIEW · Seen Jan 2024
  • Minimum Stick Connection CostONSITE INTERVIEW · Seen Jan 2024
  • Shortest Substring with at Least K Distinct CharactersONSITE INTERVIEW · Seen Jan 2024
  • Roman to IntegerPHONE SCREEN · Seen Oct 2024
  • Sort ColorsPHONE SCREEN · Seen Oct 2024
  • Basic CalculatorPHONE SCREEN · Seen Jun 2024
  • LRU CachePHONE SCREEN · Seen Jun 2024
  • House RobberPHONE SCREEN · Seen May 2024

Similar questions at other companies

  • Filesystem Entity Total SizeGoogle · ONSITE INTERVIEWGoogle interview questions
  • Render a Comment HierarchyAmazon · ONSITE INTERVIEW
  • Kth Smallest Element in a Binary Search TreeSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Flatten and Reconstruct an Ordered TreeThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
  • Inorder, Preorder and Postorder TraversalsAMD · ONSITE INTERVIEWAMD interview questions
  • Flatten JSON into Repeated Key-Value PairsKlaviyo · ONSITE INTERVIEWKlaviyo interview questions
  • Kth Smallest Value in a BSTExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
  • Validate Binary Search TreeInMobi · ONSITE INTERVIEWInMobi interview questions
  • Serialize a Binary Search TreeConfluent · ONSITE INTERVIEWConfluent interview questions
All Navan interview questionsSee Navan hiring insights
Saved
class Solution {
    public boolean sameNaryLeafValues(int[] parent1, int[] values1, int[] parent2, int[] values2) {
        // Write your code here.
    }
}
Checking account…