FastPrepSwim in Rising Water
Problems/Swim in Rising Water

Swim in Rising Water

UiPath logoUiPath● HardFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: In the first DSA round, the candidate was given two hard questions in 50 minutes. They said both were small changes to well-known problems, and this one was Swim in Rising Water.

The report does not say what the change was. Here you get the standard version: an n x n grid where every height from 0 to n * n - 1 appears once, moves go up, down, left or right, and you return the earliest time you can reach the bottom-right cell.

The judged task matches the report at about 70%.

You are given an n x n grid of integers. grid[r][c] is the height of the ground at row r and column c.

Rain starts at time 0. At time t, the water covers every cell whose height is at most t.

You start at the top-left cell (0, 0). At time t, you may swim from a cell to a cell next to it (up, down, left, or right) only if both cells have height at most t. Swimming takes no time, so you can cross any number of cells at the same moment.

Return the smallest time t at which you can reach the bottom-right cell (n - 1, n - 1).

Function

swimInWater(grid: int[][]) → int

Examples

Example 1

grid = [[0,10,11,12],[1,9,14,13],[2,3,15,4],[8,7,6,5]]return = 7

At time 6, the cells you can reach from the start have heights 0, 1, 2 and 3. Every cell next to them has height 7 or more, so you are stuck.

At time 7, the cell with height 7 is covered. You can swim along heights 0, 1, 2, 3, 7, 6, 5 and reach the bottom-right cell, so the answer is 7.

Example 2

grid = [[3,2],[0,1]]return = 3

You cannot leave the start cell until it is covered, and its height is 3. At time 3, every cell is covered, so you can swim straight to the bottom-right cell. The answer is 3.

Example 3

grid = [[0]]return = 0

The start cell is also the bottom-right cell. Its height is 0, so you are already there at time 0.

Constraints

  • n == grid.length == grid[i].length
  • 1 <= n <= 50
  • 0 <= grid[i][j] <= n * n - 1
  • All values in grid are different, so every number from 0 to n * n - 1 appears exactly once.

More UiPath problems

  • Bus RoutesPHONE SCREEN · Seen Nov 2026
  • New 21 Game ProbabilityPHONE SCREEN · Seen Sep 2026
  • Excel Sheet Column TitlePHONE SCREEN · Seen May 2026
See UiPath hiring insights
Saved
class Solution {
    public int swimInWater(int[][] grid) {
        // Write your code here.
    }
}
Checking account…