Path with Maximum Probability
Problem Statement
You are given n nodes numbered 0 to n − 1, and a list of undirected links. Link i joins the two nodes in edges[i] = [a, b], and it works with probability succProb[i].
A route works only if every link on it works, so its chance is the product of its links' chances. Find the route from start_node to end_node with the highest chance, and return that chance. If no route exists, return 0. An answer within 1e-5 of the true value is accepted.
Write double maxProbability(int n, vector<vector<int>>& edges, vector<double>& succProb, int start_node, int end_node) inside class Solution.
n, the number of nodes. edges, the links. succProb, the chance of each link (same order as edges). start_node and end_node.
The highest chance of a route from start_node to end_node, or 0 if there is none.
The route 0 → 1 → 2 has chance 0.5 × 0.5 = 0.25. The direct link has only 0.2.
Now the direct link (0.3) beats 0.5 × 0.5 = 0.25.
No route reaches node 2.
- • 2 ≤ n ≤ 10^4
- • 0 ≤ start_node, end_node < n
- • start_node ≠ end_node
- • 0 ≤ a, b < n
- • a ≠ b
- • 0 ≤ succProb.length == edges.length ≤ 2 × 10^4
- • 0 ≤ succProb[i] ≤ 1
- • There is at most one edge between every two nodes.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Multiply along every link, both directions, until a full round changes nothing
Pop the most likely route first; the first pop of the end node is the answer
Start with best[start] = 1 and 0 everywhere else. Go through every link in both directions: if best[a] × p > best[b], update best[b]. Repeat full rounds until one round changes nothing. Then return best[end].
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Shortest Paths (Weighted) translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.