FastPrepLongest Name Chain in a Directed Acyclic Graph
Problems/Longest Name Chain in a Directed Acyclic Graph

Longest Name Chain in a Directed Acyclic Graph

Oscar Health logoOscar Health● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report supplies directed name pairs, guarantees an acyclic graph, defines the result in names rather than edges, and gives a three-name example. It omits duplicate-edge behavior, bounds, and the empty-input result; the practice contract defines those details. The judged task matches the report at about 94%.

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

  • Members Lacking Provider Network AccessONSITE INTERVIEW · Seen Apr 2026
See Oscar Health hiring insights
Saved
class Solution {
    public int longestNameChain(String[][] edges) {
        // Write your solution here.
    }
}
Checking account…