Swim in Rising Water
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given an n x n integer grid grid. Each cell grid[r][c] holds a distinct elevation value.
It begins to rain. At time t, the water level across the entire grid is exactly t. You can swim from one cell to any of its four direct neighbors (up, down, left, or right) **only if** the water level is at least as high as the elevations of both the cell you are leaving and the cell you are entering. In other words, at a given time t you may stand on or move through any cell whose elevation is <= t. Moving between two adjacent reachable cells is instantaneous and you may swim any distance within a single instant.
Starting at the top-left cell (0, 0), return the **least time** t at which it is possible to reach the bottom-right cell (n - 1, n - 1).
The answer is the smallest t such that there exists a path from the start to the destination where every cell on the path has elevation <= t. Equivalently, you want to find the path that minimizes the maximum elevation encountered, and that maximum is the answer.
Examples
Constraints
- `n == grid.length == grid[i].length`
- `1 <= n <= 50`
- `0 <= grid[i][j] < n * n`
- Every value in `grid` is unique (each elevation appears exactly once).
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.