Return the Root Object from Unordered N-ary Nodes

Quick Overview

Return the Root Object from Unordered N-ary Nodes. The native vector<Node*> contains every object of one N-ary tree in arbitrary order.

Return the Root Object from Unordered N-ary Nodes

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem The native vector<Node*> contains every object of one N-ary tree in arbitrary order. Node values are unique. Return the original root Node pointer from that vector. The native driver constructs the tree from its level-order serialization,shuffles the node-object vector,and serializes the returned root to compare against the input tree. Do not replace the pointer interface with a value-only result. ```cpp class Solution { public: Node* findRoot(vector<Node*> tree); }; ``` ### Examples **Example 1:** ```text Input tree: [5,null,2,8]; possible passed order: [Node(8),Node(5),Node(2)] Output tree: [5,null,2,8] Explanation: Node(5) owns children2 and8 and is the original root object returned. ``` **Example 2:** ```text Input tree: [0]; passed vector: [Node(0)] Output tree: [0] Explanation: The only native node is the root; value0 is not an absence marker. ``` ### Constraints - The tree contains 1 to 50000 nodes. - Each native Node has a unique int value and a vector of child Node pointers. - The input vector contains all nodes of one N-ary tree in arbitrary order; the driver compares the returned root serialization with the input tree. ### Contract considerations The arbitrary order belongs to the input vector,not to the tree's child lists; preserve all native children and object identities. ```hint Read the boundary condition Check a single-node tree, arbitrary input-vector order, and the exact returned object identity. Preserve the existing children. ``` ### Boundary reminders The arbitrary order belongs to the input vector,not to the tree's child lists; preserve all native children and object identities. ### Optional extensions Can you use a linear-time algorithm with constant auxiliary workspace? Account separately for the original by-value vector parameter.

Overview: Return the Root Object from Unordered N-ary Nodes. The native vector<Node*> contains every object of one N-ary tree in arbitrary order.

Bloomberg logo
Bloomberg
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

The native vector<Node*> contains every object of one N-ary tree in arbitrary order. Node values are unique. Return the original root Node pointer from that vector. The native driver constructs the tree from its level-order serialization,shuffles the node-object vector,and serializes the returned root to compare against the input tree. Do not replace the pointer interface with a value-only result.

class Solution {
public:
    Node* findRoot(vector<Node*> tree);
};

Examples

Example 1:

Input tree: [5,null,2,8]; possible passed order: [Node(8),Node(5),Node(2)]
Output tree: [5,null,2,8]
Explanation: Node(5) owns children2 and8 and is the original root object returned.

Example 2:

Input tree: [0]; passed vector: [Node(0)]
Output tree: [0]
Explanation: The only native node is the root; value0 is not an absence marker.

Constraints

  • The tree contains 1 to 50000 nodes.
  • Each native Node has a unique int value and a vector of child Node pointers.
  • The input vector contains all nodes of one N-ary tree in arbitrary order; the driver compares the returned root serialization with the input tree.

Contract considerations

The arbitrary order belongs to the input vector,not to the tree's child lists; preserve all native children and object identities.

Boundary reminders

The arbitrary order belongs to the input vector,not to the tree's child lists; preserve all native children and object identities.

Optional extensions

Can you use a linear-time algorithm with constant auxiliary workspace? Account separately for the original by-value vector parameter.