Challenge - 5 Problems
Maze Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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 Javascript
function ratInMaze(maze) {
const n = maze.length;
const path = [];
function solve(x, y) {
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]);
maze[x][y] = 0; // mark visited
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));Attempts:
2 left
💡 Hint
Trace the path by moving down first, then right, marking visited cells.
✗ Incorrect
The rat starts at (0,0). It moves down to (1,0), then right to (1,1), down to (2,1), then down to (3,1), right to (3,2), and finally right to (3,3).
🧠 Conceptual
intermediateUnderstanding Backtracking in Rat in a Maze
Which statement best describes the role of backtracking in the Rat in a Maze problem?
Attempts:
2 left
💡 Hint
Think about what happens when the rat cannot move forward.
✗ Incorrect
Backtracking means the rat goes back one step when it cannot proceed further, trying other possible paths until it finds the exit.
🔧 Debug
advancedIdentify the Error in Maze Solver Code
What error will the following code produce when executed?
DSA Javascript
function ratInMaze(maze) {
const n = maze.length;
const path = [];
function solve(x, y) {
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, 0, 0],
[1, 1, 0],
[0, 1, 1]
];
console.log(ratInMaze(maze));Attempts:
2 left
💡 Hint
Check if the visited cells are marked to prevent revisiting.
✗ Incorrect
The code does not mark visited cells as blocked, so it revisits the same cells repeatedly, causing infinite recursion.
🚀 Application
advancedNumber of Paths in a Maze with Obstacles
Given a maze represented by a 2D grid where 1 is open and 0 is blocked, how many unique paths exist from top-left to bottom-right if the rat can only move right or down?
DSA Javascript
function countPaths(maze) {
const n = maze.length;
const m = maze[0].length;
const dp = Array.from({ length: n }, () => Array(m).fill(0));
if (maze[0][0] === 0) return 0;
dp[0][0] = 1;
for (let i = 1; i < n; i++) {
dp[i][0] = maze[i][0] === 1 ? dp[i - 1][0] : 0;
}
for (let j = 1; j < m; j++) {
dp[0][j] = maze[0][j] === 1 ? dp[0][j - 1] : 0;
}
for (let i = 1; i < n; i++) {
for (let j = 1; j < m; j++) {
if (maze[i][j] === 1) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
} else {
dp[i][j] = 0;
}
}
}
return dp[n - 1][m - 1];
}
const maze = [
[1, 1, 0],
[1, 1, 1],
[0, 1, 1]
];
console.log(countPaths(maze));Attempts:
2 left
💡 Hint
Use dynamic programming to count paths from top-left to bottom-right.
✗ Incorrect
The rat can move right or down only. The code counts all unique paths avoiding blocked cells. The total paths are 3.
❓ Predict Output
expertOutput of Rat in Maze with Multiple Paths
What is the output path of the rat in the maze after running the code below?
DSA Javascript
function ratInMaze(maze) {
const n = maze.length;
const path = [];
function solve(x, y) {
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]);
maze[x][y] = 0;
if (solve(x, y + 1)) return true;
if (solve(x + 1, y)) return true;
path.pop();
maze[x][y] = 1;
}
return false;
}
solve(0, 0);
return path;
}
const maze = [
[1, 1, 1],
[0, 1, 0],
[1, 1, 1]
];
console.log(ratInMaze(maze));Attempts:
2 left
💡 Hint
The rat tries moving right first, then down.
✗ Incorrect
The rat moves right along the top row, then down at (0,1) to (1,1), then down to (2,1), and finally right to (2,2).
