Data Structures & AlgorithmsHardGoogle · Flipkart interview
Problem: Find the path from node 0 to node 3 with the maximum product of edge success probabilities (Dijkstra on −log p, or a max-heap on the product).
Problem: Find the path from node 0 to node 3 with the maximum product of edge success probabilities (Dijkstra on −log p, or a max-heap on the product).
import heapq
def best(n, edges, s, t):
g = [[] for _ in range(n)]
for (u, v), p in edges: g[u].append((v, p)); g[v].append((u, p))
prob = [0.0] * n; prob[s] = 1.0; h = [(-1.0, s)]
while h:
p, u = heapq.heappop(h); p = -p
if u == t: return round(p, 4)
if p < prob[u]: continue
for v, w in g[u]:
if p * w > prob[v]: prob[v] = p * w; heapq.heappush(h, (-prob[v], v))
return 0.0
print(best(4, [((0,1),0.9),((1,3),0.5),((0,2),0.6),((2,3),0.9),((1,2),0.8)], 0, 3))What is printed?- A. 0.45
- B. 0.54
- C. 0.648
- D. 0.6
Correct answer: C. 0.648
Candidates: 0→1→3 = 0.45; 0→2→3 = 0.54; 0→1→2→3 = 0.9 × 0.8 × 0.9 = 0.648. Multiplying probabilities ≤ 1 can only decrease a path's value, so the Dijkstra-style greedy is valid, and the longer path wins with 0.648.
More Coding Interview Problems questions
- Problem: Each bus route is a loop of stops. Find the fewest buses needed to travel from stop S to stop T (BFS over routes, not stops)…
- Problem: Hire exactly K workers; each is paid in proportion to quality, and at least their minimum wage. Minimise total pay (sort by…
- Problem: In a sorted array every value appears twice except one. Find it in O(log n) using index parity. python def single(a): lo, hi = 0…
- Problem: Split an array into contiguous parts of length at most k; every element of a part becomes that part's maximum. Maximise the final…
- Problem: Build a binary tree whose leaves (in order) are the array values; each internal node's value is (max leaf on its left) × (max…
- Problem: Longest arithmetic subsequence (not necessarily contiguous) using a dictionary of (difference → length) per index. python def…