0
0
DSA Typescriptprogramming~10 mins

Rat in a Maze Problem in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the rat can move to the next cell safely.

DSA Typescript
function isSafe(maze: number[][], x: number, y: number): boolean {
  return x >= 0 && y >= 0 && x < maze.length && y < maze[0].length && maze[x][y] [1] 1;
}
Drag options to blanks, or click blank then click option'
A>
B!=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes wrong path checks.
Not checking boundaries leads to errors.
2fill in blank
medium

Complete the code to mark the current cell as part of the solution path.

DSA Typescript
function solveMazeUtil(maze: number[][], x: number, y: number, sol: number[][]): boolean {
  if (x === maze.length - 1 && y === maze[0].length - 1) {
    sol[x][y] = [1];
    return true;
  }
  if (isSafe(maze, x, y)) {
    sol[x][y] = 1;
    // rest of code omitted
  }
  return false;
}
Drag options to blanks, or click blank then click option'
A1
B0
C-1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Marking with 0 instead of 1 causes no path to be recorded.
Using negative values is incorrect.
3fill in blank
hard

Fix the error in the recursive call to move down in the maze.

DSA Typescript
if (solveMazeUtil(maze, x [1], y, sol)) {
  return true;
}
Drag options to blanks, or click blank then click option'
A- 1
B+ 1
C* 1
D/ 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '- 1' moves up, not down.
Using '* 1' or '/ 1' does not change position.
4fill in blank
hard

Fill both blanks to move right and backtrack correctly.

DSA Typescript
if (solveMazeUtil(maze, x, y [1], sol)) {
  return true;
}
sol[x][y] = [2]; // backtracking
Drag options to blanks, or click blank then click option'
A+ 1
B- 1
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '- 1' moves left, not right.
Setting backtracking to 1 keeps the cell marked.
5fill in blank
hard

Fill all three blanks to complete the main function that initializes and starts the maze solving.

DSA Typescript
function solveMaze(maze: number[][]): number[][] | null {
  const sol = Array(maze.length).fill(null).map(() => Array(maze[0].length).fill([1]));
  if (solveMazeUtil(maze, [2], [3], sol)) {
    return sol;
  }
  return null;
}
Drag options to blanks, or click blank then click option'
A0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing solution matrix with 1 causes wrong path.
Starting from other than (0, 0) misses the maze start.