Reported September 2026
Oscar Healthdynamic programming

Longest Name Chain in a Directed Acyclic Graph

Reported by candidates from Oscar Health's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Oscar Health OA. Under 2s to a working solution.
Founder's read

Oscar Health reported this one in September 2026, and the input size is the whole story. Up to 100000 edges means any approach that re-explores paths from every starting name will time out. It's a longest path in a DAG problem dressed up as a name chain. Dedupe the edges, map names to ids, then compute the longest path once per node. If you blank on the ordering step during the OA, StealthCoder is the safety net running invisibly on your screen, but the idea is short enough to hold in your head tonight.

The problem

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.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that a DAG has no cycles, so each node's best chain can be computed once and reused. Two clean options. First, Kahn's topological sort: track indegrees, process nodes in order, and set dist[next] = max(dist[next], dist[cur] + 1), starting every node at 1. Second, DFS with memoization, where longest(node) = 1 + max(longest(child)). Answer is the max over all nodes. The pitfalls are specific. Duplicate rows must not count twice, so use a set of edges or dedupe before building adjacency. Nodes that only appear as a target still count as names. Empty input returns 0, not 1. With 100000 edges, recursive DFS can blow the stack in some languages, so prefer the iterative topological version. Complexity is O(V + E). StealthCoder is your hedge in the live OA if the memoization or indegree bookkeeping slips under pressure.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Longest Name Chain in a Directed Acyclic Graph cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Oscar Health's OA.

Oscar Health reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Name Chain in a Directed Acyclic Graph FAQ

What's the trick to Longest Name Chain in a DAG?+

Compute the longest path ending at each node once, using topological order or memoized DFS. Each node starts at length 1, and each edge relaxes the neighbor to max(current, parent + 1). Answer is the largest value. That gives O(V + E), which is what 100000 edges demands.

Why does brute force fail here?+

Starting a fresh DFS from every name re-walks shared sub-paths over and over. In a dense DAG that blows up exponentially. With up to 100000 edges you need each node's result computed exactly once and cached.

How do I handle duplicate edges?+

Dedupe before building the graph. Put each pair in a set, or check adjacency sets when inserting. If you skip this, indegree counts in Kahn's algorithm get inflated and nodes may never reach zero, giving a wrong answer.

What edge cases should I test?+

Empty edges returns 0. A single edge returns 2. Disconnected components, where the answer is the longest one. Diamond shapes like Example 2, where two branches merge. Names that appear only as a destination, which still need a node entry.

How do I prepare for this in 48 hours?+

Write Kahn's topological sort and the memoized DFS once each from scratch, then run Example 2 by hand. Practice mapping strings to integer ids with a hash map. Know that this is the same idea as longest path in a DAG, and you're covered for this pattern.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Oscar Health.

OA at Oscar Health?
Invisible during screen share
Get it