FastPrepLongest Name Chain in a Directed Acyclic Graph

Longest Name Chain in a Directed Acyclic Graph

Oscar Health logoOscar Health● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Each row [from, to] in edges is a directed connection between two names. The complete graph is guaranteed to be acyclic.

A name chain is a directed path. Its length is the number of names on that path, including both endpoints. Return the maximum chain length. Duplicate rows describe the same directed edge and must not increase the result. Return 0 when edges is empty.

Function

longestNameChain(edges: String[][]) → int

Examples

Example 1

edges = [["a","b"],["b","c"],["d","e"]]return = 3

The longest chain is a -> b -> c, which contains three names.

Example 2

edges = [["amy","bo"],["amy","cy"],["bo","dee"],["cy","dee"],["dee","eve"]]return = 4

Either branch from amy reaches eve through four names.

Example 3

edges = []return = 0

No names are present.

Constraints

  • 0 <= edges.length <= 100000.
  • Every row contains exactly two nonempty names.
  • Names contain 1 to 40 visible ASCII characters.
  • The graph described by the distinct edges is a directed acyclic graph.

More Oscar Health problems

See Oscar Health hiring insights
public int longestNameChain(String[][] edges) {
    // Write your solution here.
}
edges[["a","b"],["b","c"],["d","e"]]
expected3
Checking account…