Compute minimax grid path and network delay
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving in shortest-path and path-optimization contexts, specifically minimax pathfinding on a grid and single-source reachability/timing in a weighted directed graph, testing graph modeling, alternative path cost metrics, edge-case handling, and working with large numeric weights.
Part 1: Minimize the Maximum Value Along a Grid Path
Constraints
- 1 <= m, n <= 200
- 0 <= H[r][c] <= 10^9
Examples
Input: [[1, 10, 6], [1, 3, 2], [7, 2, 2]]
Expected Output: 3
Explanation: A best path is `(0,0) -> (1,0) -> (1,1) -> (1,2) -> (2,2)`, which visits values `1, 1, 3, 2, 2`. The maximum is `3`, and no path can do better.
Input: [[5, 1, 7], [4, 8, 2], [3, 6, 0]]
Expected Output: 6
Explanation: Going down the left side and then across the bottom gives values `5, 4, 3, 6, 0`, so the path cost is `6`. Any route through the top or middle forces a maximum of at least `7` or `8`.
Input: [[0, 2], [1, 3]]
Expected Output: 3
Explanation: Both possible paths must end at the cell with value `3`, so the minimum possible maximum is `3`.
Input: [[9, 1], [2, 3]]
Expected Output: 9
Explanation: The starting cell counts toward the path cost. Since the start already has value `9`, the answer cannot be smaller than `9`.
Input: [[42]]
Expected Output: 42
Explanation: Start and end are the same cell, so the path cost is just that cell's value.
Hints
- Think of the 'distance' to a cell as the smallest possible maximum value seen on any path to that cell.
- A min-heap can help you always expand the cell with the best current minimax cost first.
Part 2: Network Delay Time in a Directed Weighted Graph
Constraints
- 1 <= n <= 10^5
- 0 <= len(edges) <= 2 * 10^5
- 1 <= u, v <= n
- 1 <= w <= 10^9
- The graph is directed.
- There may be multiple edges between the same pair of nodes.
Examples
Input: ([[2, 1, 1], [2, 3, 1], [3, 4, 1]], 4, 2)
Expected Output: 2
Explanation: From node `2`, the signal reaches nodes `1` and `3` in `1` unit, and node `4` in `2` units. The last arrival time is `2`.
Input: ([[1, 2, 1]], 3, 1)
Expected Output: -1
Explanation: Node `3` is unreachable from node `1`, so the answer is `-1`.
Input: ([[1, 2, 5], [1, 2, 1], [2, 3, 2]], 3, 1)
Expected Output: 3
Explanation: There are two edges from `1` to `2`; the shorter one with weight `1` should be used. Then node `3` is reached in `1 + 2 = 3`.
Input: ([], 1, 1)
Expected Output: 0
Explanation: There is only one node, and it is the starting node, so the signal is already everywhere at time `0`.
Input: ([[1, 2, 10], [1, 3, 1], [3, 2, 1]], 3, 1)
Expected Output: 2
Explanation: Although node `2` is first discovered with distance `10`, a shorter path `1 -> 3 -> 2` gives distance `2`. The farthest node is reached at time `2`.
Hints
- Because all edge weights are positive, Dijkstra's algorithm is a strong fit.
- After computing shortest distances, check whether any node is still unreached before taking the maximum distance.