Minimum Moves
Learn this problemProblem statement
There is a maze in HackerPlay where children play for recreation.
The maze is represented as an n x m grid of cells, where each cell is either empty (denoted by 0), or contains an obstacle (denoted by 1). HackerMan is currently standing at cell (0, 0), and wishes to reach the cell (n - 1, m - 1).
For a jump parameter denoted by k, in one move, HackerMan can move to any of the following cells:
(i + k, j)where1 ≤ k ≤ nprovided cell(i + k, j)lies in the maze and there are no cells containing obstacles in the range(i + 1, j)to(i + k, j).(i, j + k)where1 ≤ k ≤ mprovided cell(i, j + k)lies in the maze and there are no cells containing obstacles in the range(i, j + 1)to(i, j + k).(i - k, j)where1 ≤ k ≤ nprovided cell(i - k, j)lies in the maze and there are no cells containing obstacles in the range(i - 1, j)to(i - k, j).(i, j - k)where1 ≤ k ≤ mprovided cell(i, j - k)lies in the maze and there are no cells containing obstacles in the range(i, j - 1)to(i, j - k).
Determine the minimum number of moves in which HackerMan can reach the cell (n - 1, m - 1) starting from (0, 0), or -1 if it is impossible to reach that cell.
Function
getMinimumMoves(maze: int[][], k: int) → int
Complete the function getMinimumMoves in the editor.
getMinimumMoves has the following parameters:
int maze[n][m]: the maze in HackerPlay where HackerMan is standingint k: the maximum distance HackerMan can traverse in one move
Returns
int: the minimum number of moves in which HackerMan can reach the destination cell (n - 1, m - 1)
Examples
Example 1
maze = [[0, 1], [1, 0]]k = 2return = 2The maze looks like this.
[[0, 0],
[1, 0]]
The following sequence of moves can be performed: (0, 0) to (0, 1) to (1, 1). Hence, HackerMan can reach the end in 2 moves, which is minimum possible. The answer is 2.
Example 2
maze = [[0, 0, 0], [1, 0, 0], [1, 0, 0]]k = 5return = 2The maze can be represented as:
[[0, 0, 0],
[1, 0, 0],
[1, 0, 0]]
The following sequence of moves can be performed: (0, 0) to (0, 2) to (1, 2).
Example 3
maze = [[0, 1, 0], [1, 0, 0], [1, 0, 0]]k = 5return = -1The maze can be represented as:
[[0, 1, 0],
[1, 1, 0],
[1, 0, 0]]
HackerMan is blocked and cannot move from the cell (0, 0).
Constraints
1 ≤ n, m ≤ 1001 ≤ k ≤ 100More Goldman Sachs problems
- Data ReorganizationSeen Jul 2026
- Inherited Role PermissionsONSITE INTERVIEW · Seen Jul 2026
- Root of the Largest TreePHONE SCREEN · Seen Jul 2026
- Validate Binary Search TreeONSITE INTERVIEW · Seen Jul 2026
- Alternating Parity PermutationsOA · Seen Jul 2026
- Threshold AlertsSeen Jul 2026
- Cheapest Flights Within K StopsONSITE INTERVIEW · Seen Jun 2026
- Word LadderPHONE SCREEN · Seen Jun 2026