#1514 Path with Maximum Probability
frequently reported atGoogle
the problem
In plain words
An undirected graph has n nodes. edges[i] = [a, b] succeeds with probability succProb[i]. Return the highest success probability of any path from start to end, or 0 if there is no path.
in n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
out 0.25
0→1→2 = 0.5 × 0.5 = 0.25 beats 0→2 = 0.2.
in n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2
out 0.0
- 2 ≤ n ≤ 10⁴
- 0 ≤ edges.length ≤ 2·10⁴
- 0 ≤ succProb[i] ≤ 1
- At most one edge between two nodes
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternDijkstra
Multiplying by a number ≤ 1 can never increase a probability — the same way adding a weight ≥ 0 can never decrease a distance. That's exactly the condition Dijkstra needs, just with a max-heap and × instead of +.
solutions
From brute force to optimal
Repeat: for every edge (both directions) try prob[a] × p > prob[b]. Stop when nothing changes.
- prob[start] = 1.
- Up to n − 1 rounds over all edges, both directions.
- Return prob[end].
Up to n − 1 rounds × E edges; ~2·10⁸ in the worst case.
The prob array.
class Solution { public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) { double[] prob = new double[n]; // 0 = not reachable yet prob[start] = 1.0; for (int round = 1; round <= n - 1; round++) { boolean changed = false; for (int i = 0; i < edges.length; i++) { int a = edges[i][0], b = edges[i][1]; double p = succProb[i]; if (prob[a] * p > prob[b]) { prob[b] = prob[a] * p; changed = true; } // undirected: if (prob[b] * p > prob[a]) { prob[a] = prob[b] * p; changed = true; } // try both ways } if (!changed) break; } return prob[end]; }}Always expand the most likely node; when end is polled, its probability is final.
- Undirected adjacency list with probabilities.
- Max-heap of {prob, node}; start {1.0, start}.
- Poll; if it's
end, return. Skip stale entries. - Relax: candidate = p × edgeProb; push if it beats prob[v].
Standard Dijkstra bound.
Adjacency list and heap.
class Solution { public double maxProbability(int n, int[][] edges, double[] succProb, int start, int end) { List<List<double[]>> adj = new ArrayList<>(); for (int i = 0; i < n; i++) adj.add(new ArrayList<>()); for (int i = 0; i < edges.length; i++) { adj.get(edges[i][0]).add(new double[] {edges[i][1], succProb[i]}); adj.get(edges[i][1]).add(new double[] {edges[i][0], succProb[i]}); } double[] prob = new double[n]; prob[start] = 1.0; // Max-heap on probability: the most likely node is final when it is polled. PriorityQueue<double[]> pq = new PriorityQueue<>((x, y) -> Double.compare(y[0], x[0])); pq.offer(new double[] {1.0, start}); while (!pq.isEmpty()) { double[] top = pq.poll(); double p = top[0]; int u = (int) top[1]; if (u == end) return p; // first time end is polled = best if (p < prob[u]) continue; // stale entry for (double[] next : adj.get(u)) { int v = (int) next[0]; double candidate = p * next[1]; if (candidate > prob[v]) { prob[v] = candidate; pq.offer(new double[] {candidate, v}); } } } return 0.0; // end is unreachable }}in the room
How to say it in the interview
talk track
- Say why Dijkstra is legal here: "multiplying by ≤ 1 is monotone, like adding ≥ 0."
- Mention the log trick: maximise product ⇔ minimise Σ −log(p), all ≥ 0.
traps
- Using a min-heap out of habit.
- Forgetting the graph is undirected — add both directions.
what they ask next
Follow-ups
Could you turn it into a normal shortest path?
Yes: weight = −log(p) ≥ 0. Then the shortest path maximises the product. Doubles make the direct version simpler, though.
before you submit
Edge cases to test
- No path from start to end → 0.0.
- An edge with probability 0 is as good as no edge.
- A longer path can win: 0.5 × 0.5 = 0.25 beats a direct 0.2.
practise next
Similar problems
- #743Network Delay TimeMediumin Shortest Paths
- #1631Path With Minimum EffortMediumin Shortest Paths