FastPrepFind Root IDs from Parent References
Problems/Find Root IDs from Parent References

Find Root IDs from Parent References

Zip logoZip● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for root IDs from JSON blocks containing id and parentId. It omits null encoding, missing-parent behavior, and ordering; the judged core task matches the available source at about 88%. A path-returning follow-up lacked an endpoint contract and is not graded.

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

  • Maximum Survival Time in a Graph PursuitPHONE SCREEN · Seen Sep 2026
  • Find All Zip Rummy MeldsPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2026
  • Shortest Path Around Moving ParadesPHONE SCREEN · Seen Mar 2026
All Zip interview questionsSee Zip hiring insights
Saved
class Solution {
    public String[] findRootIds(String[][] nodes) {
        // Write your code here.
    }
}
Checking account…