0
0
DSA Typescriptprogramming~20 mins

Rat in a Maze Problem in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Maze Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Rat's Path in a Simple Maze
What is the output path of the rat in the maze after running the given code?
DSA Typescript
function ratInMaze(maze: number[][], n: number): string[] {
  const path: string[] = [];
  function solve(x: number, y: number): boolean {
    if (x === n - 1 && y === n - 1) {
      path.push(`${x},${y}`);
      return true;
    }
    if (x >= 0 && y >= 0 && x < n && y < n && maze[x][y] === 1) {
      maze[x][y] = 0; // mark visited
      path.push(`${x},${y}`);
      if (solve(x + 1, y)) return true;
      if (solve(x, y + 1)) return true;
      path.pop();
      maze[x][y] = 1; // backtrack
    }
    return false;
  }
  solve(0, 0);
  return path;
}

const maze = [
  [1, 0, 0, 0],
  [1, 1, 0, 1],
  [0, 1, 0, 0],
  [1, 1, 1, 1]
];
console.log(ratInMaze(maze, 4));
A["0,0","1,0","1,1","1,2","2,2","3,2","3,3"]
B["0,0","1,0","1,1","2,1","3,1","3,2","3,3"]
C["0,0","1,0","2,0","3,0","3,1","3,2","3,3"]
D["0,0","0,1","1,1","2,1","3,1","3,2","3,3"]
Attempts:
2 left
💡 Hint
Trace the path by moving down first, then right, marking visited cells.
🧠 Conceptual
intermediate
1:00remaining
Understanding Rat in a Maze Movement Constraints
In the Rat in a Maze problem, which movement directions are typically allowed for the rat to find a path?
ADiagonal moves only
BUp, Down, Left, Right
CDown and Right only
DUp and Left only
Attempts:
2 left
💡 Hint
Check the common constraints in classic Rat in a Maze problems.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Maze Pathfinding Code
What error will the following TypeScript code produce when run, and why? function ratInMaze(maze: number[][], n: number): string[] { const path: string[] = []; function solve(x: number, y: number): boolean { if (x === n - 1 && y === n - 1) { path.push(`${x},${y}`); return true; } if (x >= 0 && y >= 0 && x < n && y < n && maze[x][y] === 1) { path.push(`${x},${y}`); if (solve(x + 1, y)) return true; if (solve(x, y + 1)) return true; path.pop(); } return false; } solve(0, 0); return path; } const maze = [ [1, 1], [1, 1] ]; console.log(ratInMaze(maze, 2));
ARuntime error: Cannot read property of undefined
BCorrect output: ["0,0","1,0","1,1"]
CEmpty path returned []
DStack overflow due to infinite recursion
Attempts:
2 left
💡 Hint
Check if the code marks visited cells to avoid revisiting.
🚀 Application
advanced
1:30remaining
Number of Paths in a Maze with Obstacles
Given a 3x3 maze represented as a grid where 1 is open and 0 is blocked: [[1,1,0], [1,1,1], [0,1,1]] How many unique paths exist from top-left (0,0) to bottom-right (2,2) moving only down or right?
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Count all possible routes avoiding blocked cells moving only down or right.
Predict Output
expert
2:30remaining
Output of Rat in Maze with Backtracking and Multiple Paths
What is the output of the following TypeScript code that finds one path in a 4x4 maze with multiple possible routes? function ratInMaze(maze: number[][], n: number): string[] { const path: string[] = []; function solve(x: number, y: number): boolean { if (x === n - 1 && y === n - 1) { path.push(`${x},${y}`); return true; } if (x >= 0 && y >= 0 && x < n && y < n && maze[x][y] === 1) { maze[x][y] = 0; // mark visited path.push(`${x},${y}`); if (solve(x, y + 1)) return true; if (solve(x + 1, y)) return true; path.pop(); maze[x][y] = 1; // backtrack } return false; } solve(0, 0); return path; } const maze = [ [1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1] ]; console.log(ratInMaze(maze, 4));
A["0,0","0,1","1,1","1,2","2,2","2,3","3,3"]
B["0,0","1,0","1,1","2,1","2,2","3,2","3,3"]
C["0,0","0,1","0,2","1,2","2,2","3,2","3,3"]
D["0,0","0,1","1,1","2,1","3,1","3,2","3,3"]
Attempts:
2 left
💡 Hint
The code tries moving right first, then down.