FastPrepSort Every N-ary Tree Node's Children

Sort Every N-ary Tree Node's Children

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

An N-ary tree is encoded by parallel arrays. Node i has value values[i], and children[i] contains the indices of its children.

Return a copy of children where every row is sorted by ascending child-node value, breaking equal-value ties by ascending child index.

Function

sortNaryChildren(values: int[], children: int[][]) → int[][]

Examples

Example 1

values = [10,5,7,5]children = [[2,1,3],[],[],[]]return = [[1,3,2],[],[],[]]

Children with value 5 come before value 7; indices 1 then 3 break the tie.

Constraints

  • 1 <= values.length == children.length <= 10^5.
  • Child relationships form one rooted tree with root index 0.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[][] sortNaryChildren(int[] values, int[][] children) {
  // Write your code here.
}
values[10,5,7,5]
children[[2,1,3],[],[],[]]
expected[[1,3,2],[],[],[]]
Checking account…