FastPrepFind Root IDs from Parent References

Find Root IDs from Parent References

Zip logoZip● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Each row in nodes represents one JSON-like object as [id, parentId]. Every id is unique. An empty parentId represents null.

A node is a root when its parentId is empty or when that parent ID does not appear among the supplied node IDs. Return all root IDs in lexicographic order.

Function

findRootIds(nodes: String[][]) → String[]

Examples

Example 1

nodes = [["a",""],["b","a"],["c","a"],["d","missing"]]return = ["a","d"]

Node a has no parent, and d references a parent that is absent. Nodes b and c reference a.

Example 2

nodes = [["child","root"],["root",""]]return = ["root"]

Input order does not matter; root is the only node without a present parent.

Example 3

nodes = [["x","y"],["y","x"]]return = []

Both parent references are present, so this cycle contains no root under the stated rule.

Constraints

  • 0 <= nodes.length <= 100000.
  • nodes[i].length == 2.
  • Each id is a distinct non-empty string.
  • Each parentId is either empty or a non-empty string.
  • The combined length of all strings is at most 10^6.

More Zip problems

See Zip hiring insights
public String[] findRootIds(String[][] nodes) {
    // Write your code here.
}
nodes[["a",""],["b","a"],["c","a"],["d","missing"]]
expected["a", "d"]
Checking account…