Amazon | Onsite | Longest Hierarchy

Location: India
I was asked the following question:

Given a set of pair of nodes of a graph, find the longest hierarchy that can be formed in the graph. By hierarchy, we mean that the destination of one pair of nodes should be the source of other pair of nodes e.g., Consider the following set:
{(a,b), (b,c), (c,d)}
In the above set, the longest hierarchy is abc. This can be derived considering a as source initially and then deriving the hierarchy further like a -> b -> c.

I was unable to think of a solution initially but have realized that the question could be solved using DFS. Is this a correct approach to the question? What other approaches are poosible/efficient? Any sort of help is highly appreciated.

Comments (5)