Compute earliest arrival in time-grid
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving and pathfinding skills on grid-based graphs, focusing on modeling time-dependent traversal constraints and reasoning about efficient search and supporting data structures.
Constraints
- n == grid.length == grid[i].length
- 1 <= n <= 50
- 0 <= grid[i][j] < n * n
- Each value in the range [0, n*n - 1] appears exactly once in the grid (a permutation), though the algorithm does not rely on this.
- You start at (0, 0) and must reach (n-1, n-1).
Examples
Input: ([[0,2],[1,3]],)
Expected Output: 3
Explanation: Path (0,0)->(1,0)->(1,1) has max value 3; you must wait until time 3 to enter cell (1,1).
Input: ([[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]],)
Expected Output: 16
Explanation: The optimal route snakes through the grid with a bottleneck cell of value 16, so the earliest arrival is time 16.
Input: ([[0]],)
Expected Output: 0
Explanation: Single cell: start and end coincide; the answer is the start cell's value 0.
Input: ([[3,2],[0,1]],)
Expected Output: 3
Explanation: Every path from (0,0) to (1,1) must pass through the start cell of value 3, so the bottleneck is 3.
Input: ([[0,1,2,3,4,24,23,22],[21,20,19,18,17,16,15,5],[14,13,12,11,10,9,8,6],[7,1,2,3,4,5,6,7],[8,9,10,11,12,13,14,8],[15,16,17,18,19,20,21,9],[22,23,24,25,26,27,28,10],[29,30,31,32,33,34,35,11]],)
Expected Output: 17
Explanation: Larger grid: the cheapest minimax route to the bottom-right has a maximum cell value of 17.
Input: ([[10,12],[13,11]],)
Expected Output: 12
Explanation: From (0,0)=10, going right to (0,1)=12 then down to (1,1)=11 has max 12; going down via (1,0)=13 has max 13. The better bottleneck is 12.
Hints
- Reframe the problem: the time to traverse a path equals the maximum cell value along that path. You want the path whose maximum value is as small as possible (a minimax / bottleneck path problem).
- A Dijkstra-style search works: maintain a min-heap keyed by 'the largest cell value seen so far on the best route to this cell'. Pop the smallest such key and relax neighbors with max(currentTime, neighborValue).
- Alternatives: binary search on the answer T plus a BFS/DFS checking whether (n-1,n-1) is reachable using only cells <= T; or Union-Find, adding cells in increasing value order until (0,0) and (n-1,n-1) become connected.
- Mark a cell visited when you first push it (its first pop is already optimal under the minimax key), so each cell is processed once.