Compute sums and max path in N-ary tree
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates implementation and traversal skills for N-ary tree data structures, including recursion/DFS techniques, aggregation of node values, and tracking root-to-leaf path sums.
Sum of an N-ary Tree
Constraints
- Each non-empty node is [value, children]
Examples
Input: ([1, [[2, []], [3, [[4, []]]]]],)
Expected Output: 10
Explanation: Nested children.
Input: (None,)
Expected Output: 0
Explanation: Empty tree.
Input: ([-5, []],)
Expected Output: -5
Explanation: Single negative node.
Hints
- DFS every node exactly once.
Maximum Root-to-Leaf Path Sum in an N-ary Tree
Constraints
- Each non-empty node is [value, children]
Examples
Input: ([1, [[2, []], [3, [[4, []], [-10, []]]]]],)
Expected Output: 8
Explanation: Chooses 1->3->4.
Input: (None,)
Expected Output: None
Explanation: Empty tree.
Input: ([-2, [[-3, []], [-1, []]]],)
Expected Output: -3
Explanation: All negative values.
Hints
- The best path from a node is its value plus the best child path.
Return a Maximum Root-to-Leaf Path
Constraints
- Ties may return any maximum path; this solution chooses the first best child.
Examples
Input: ([1, [[2, []], [3, [[4, []], [-10, []]]]]],)
Expected Output: [1, 3, 4]
Explanation: Returns values on the best path.
Input: (None,)
Expected Output: []
Explanation: Empty tree.
Input: ([5, []],)
Expected Output: [5]
Explanation: Single node path.
Hints
- Compute the best child path recursively and prepend the current value.