Path With Maximum Probability
Updated
Problem statement
You are given an undirected graph with n nodes numbered from 0 through n - 1. Edge edges[i] connects two nodes and succeeds with probability probabilities[i].
The probability of a path is the product of the probabilities of its edges. Return the maximum probability of reaching end from start.
- If no path exists, return
0.0. - If
start == end, return1.0.
Function
maxProbability(n: int, edges: int[][], probabilities: double[], start: int, end: int) → doubleExamples
Example 1
n = 3edges = [[0,1],[1,2],[0,2]]probabilities = [0.5,0.5,0.2]start = 0end = 2return = 0.25The path 0 -> 1 -> 2 has probability 0.5 * 0.5 = 0.25, which is greater than the direct edge probability 0.2.
Example 2
n = 3edges = [[0,1],[1,2],[0,2]]probabilities = [0.5,0.5,0.3]start = 0end = 2return = 0.3The direct edge from 0 to 2 succeeds with probability 0.3, exceeding the two-edge path probability 0.25.
Example 3
n = 3edges = [[0,1]]probabilities = [0.5]start = 0end = 2return = 0.0Node 2 is unreachable from node 0.
Constraints
1 <= n <= 10^40 <= edges.length = probabilities.length <= 2 * 10^4- Each edge contains two distinct valid node indices.
0.0 <= probabilities[i] <= 1.00 <= start, end < n- Parallel edges are allowed.
More Salesforce Onsite Interview questions
- Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
- Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
- 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
- Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
- Group Identical Tree LevelsONSITE INTERVIEW · Seen Oct 2026
- Meeting Rooms IIONSITE INTERVIEW · Seen Oct 2026
- Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
- Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Product Path in a Directed GraphGoogle · ONSITE INTERVIEWGoogle interview questions
- Dijkstra's Shortest PathsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Single-Source Shortest PathsRippling · ONSITE INTERVIEWRippling interview questions
- Minimum-Weight Path Through a MatrixMeta · ONSITE INTERVIEWMeta interview questions
- Hub Vertices Within a RadiusThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Minimum Path Cost with Edge DiscountsSkyscanner · ONSITE INTERVIEWSkyscanner interview questions
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Shortest Paths in a Nonnegative Directed GraphNation with NaMo · PHONE SCREEN
- Dijkstra Shortest Paths in a Weighted Undirected GraphGoldman Sachs · PHONE SCREENGoldman Sachs interview questions