Path with Maximum Probability

MediumGraphsdijkstra

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an undirected weighted graph with n nodes labeled 0 to n - 1. Each edges[i] = [a, b] has a traversal success probability succProb[i], a value between 0 and 1. The success probability of a path is the product of the probabilities of its edges. Return the highest success probability of any path from node startNode to node endNode. If there is no path at all, return 0. Answers within 1e-6 of the correct value are accepted. There is at most one edge between any pair of nodes.

Examples

Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], startNode = 0, endNode = 2
Output: 0.25
The direct edge gives `0.2`, but going `0 -> 1 -> 2` gives `0.5 * 0.5 = 0.25`.
Input: n = 3, edges = [[0,1]], succProb = [0.5], startNode = 0, endNode = 2
Output: 0.0
Node 2 is unreachable from node 0.

Constraints

  • 2 <= n <= 10^4
  • 0 <= edges.length <= 2 * 10^4
  • `edges[i] = [a, b]`, `a != b`, no duplicate edges
  • 0 <= succProb[i] <= 1
  • 0 <= startNode, endNode < n, startNode != endNode

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More dijkstra problems