Find minimum threshold enabling grid path
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's understanding of grid pathfinding, reachability under constraints, and algorithmic optimization, focusing on skills such as graph traversal and handling threshold-based feasibility.
Constraints
- 1 <= m, n <= 200
- 1 <= m * n <= 40000
- -10^9 <= grid[i][j] <= 10^9
- Movement is allowed only in 4 directions: up, down, left, and right
Examples
Input: ([[0, 2], [1, 3]],)
Expected Output: 3
Explanation: Any path to the bottom-right must include the cell with value 3, so the minimum valid threshold is 3.
Input: ([[0, 100, 2], [1, 2, 2], [3, 4, 1]],)
Expected Output: 2
Explanation: A valid path is 0 -> 1 -> 2 -> 2 -> 1, whose largest value is 2. The cell 100 can be avoided.
Input: ([[-5]],)
Expected Output: -5
Explanation: The start and destination are the same cell, so the threshold only needs to allow that cell.
Input: ([[-3, -2, -1], [-4, 5, 0], [-5, -6, -7]],)
Expected Output: -3
Explanation: A path through the left and bottom edges uses values -3, -4, -5, -6, -7, so the largest value on that path is -3.
Input: ([[7, 7, 7], [7, 7, 7]],)
Expected Output: 7
Explanation: Every possible path consists only of 7s, so the minimum threshold is 7.
Input: ([[5, 1, 7], [4, 8, 2], [3, 6, 0]],)
Expected Output: 6
Explanation: The best route is 5 -> 4 -> 3 -> 6 -> 0, so the minimum possible maximum value along a path is 6.
Hints
- Think of the cost of a path as the maximum cell value that appears on it, not the sum of values.
- A priority queue can help you always expand the position with the smallest currently known path cost first.