← Questions
Grid path that keeps its highest cell as low as possible
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Feb 2026. It tests graphs, dijkstra and binary-search.
Each cell of an m x n grid holds a height. Find a path from the top-left cell to the bottom-right cell, moving up, down, left or right, whose highest cell is as low as possible. Return that height: the smallest threshold t such that a path exists using only cells of height at most t.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
min_max_height(grid: list[list[int]]) -> int— the start and end cells count as part of the path.
Example
[[1, 2, 3],
[8, 9, 4],
[7, 6, 5]] -> 5 # 1-2-3-4-5 around the edge
[[5, 1],
[9, 2]] -> 5 # the start cell itself is 5
Constraints
-
1 <= m, n <= 1000. -
0 <= height <= 10^9.
Clarifying questions
- Do the start and end cells count toward the maximum?
- Four directions only, or diagonals too?
- Are heights integers, and what is their range?
What a strong answer covers
- Approach 1: binary search on the threshold; for each candidate, BFS or DFS over cells at most that high. O(mn log H), or O(mn log(mn)) searching over the sorted distinct heights.
- Approach 2: Dijkstra where a path's cost is its maximum height: a min-heap keyed by the best maximum so far, stopping when the target pops. O(mn log(mn)).
- Approach 3: sort cells by height and add them to a union-find until start and end connect.
- Whether plain DFS works: yes, as the feasibility check inside the binary search; no, as a way to find the optimum directly.
- Tests: a 1 x 1 grid, a start or end cell that is the highest cell.
Follow-ups
- Return the path itself.
- Answer many start and end queries on the same grid.
- Minimize the largest height difference between neighbouring cells on the path instead.
Where it's asked
- Google · First round: coding · Last asked Feb 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
graphs · dijkstra · binary-search
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
