Facebook | Onsite | Longest Path in weighted DAG

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2021-06-28Added By Anonymous
Source: Link

Company: Meta (Facebook)
Role: unknown
Interview Stage: Onsite
Location: unknown
Year: unknown

Original Post Content:

Given a weighted Directed Acyclic Graph, find the longest path between two given node.
I came up with a simple BFS solution, but then Googled the question after the interview and this seems to be more complicated than what I solved in the interview:

The input is given as a list of String. `a-b` means the edge from `a` to `b` and the weight it has (you don't need to parse the json),

[
  {
    "str": "a-b",
    "weigth": 2
  },
  {
    "str": "b-c",
    "weigth": 2
  },
    {
    "str": "a-d",
    "weigth": 1
  },
    {
    "str": "d-c",
    "weigth": 5
  }
]

// the answer is "a -> d -> c" with total weight of 6

You need to implement the following method:

`int longestPath(Edge[] edge) {}`

Comments