Challenge - 5 Problems
Rat in a Maze Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Rat in a Maze Path
What is the output path printed by this Rat in a Maze code when the maze is solved?
DSA C#
using System; class MazeSolver { static int N = 4; static bool IsSafe(int[,] maze, int x, int y) { return (x >= 0 && x < N && y >= 0 && y < N && maze[x, y] == 1); } static bool SolveMazeUtil(int[,] maze, int x, int y, int[,] sol) { if (x == N - 1 && y == N - 1 && maze[x, y] == 1) { sol[x, y] = 1; return true; } if (IsSafe(maze, x, y)) { if (sol[x, y] == 1) return false; sol[x, y] = 1; if (SolveMazeUtil(maze, x + 1, y, sol)) return true; if (SolveMazeUtil(maze, x, y + 1, sol)) return true; sol[x, y] = 0; return false; } return false; } public static void SolveMaze(int[,] maze) { int[,] sol = new int[N, N]; if (!SolveMazeUtil(maze, 0, 0, sol)) { Console.WriteLine("No solution"); return; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Console.Write(sol[i, j] + " "); } Console.WriteLine(); } } static void Main() { int[,] maze = { {1, 0, 0, 0}, {1, 1, 0, 1}, {0, 1, 0, 0}, {1, 1, 1, 1} }; SolveMaze(maze); } }
Attempts:
2 left
💡 Hint
Trace the path from top-left to bottom-right marking 1s where the rat can move down or right safely.
✗ Incorrect
The rat moves down first, then right, avoiding blocked cells (0). The solution matrix marks the path with 1s showing the route taken.
❓ Predict Output
intermediateNumber of Steps in Rat in a Maze Path
How many steps does the rat take in the solution path printed by the code below?
DSA C#
int[,] maze = { {1, 0, 0, 0}, {1, 1, 0, 1}, {0, 1, 0, 0}, {1, 1, 1, 1} }; // The solution path is marked by 1s in the output matrix from previous code.
Attempts:
2 left
💡 Hint
Count the number of 1s in the solution matrix representing the path.
✗ Incorrect
The path includes 7 cells marked 1 in the solution matrix, representing 7 steps from start to finish.
🔧 Debug
advancedIdentify the Error in Maze Solver Code
What error will this code produce when run?
DSA C#
static bool SolveMazeUtil(int[,] maze, int x, int y, int[,] sol) { if (x == N - 1 && y == N - 1 && maze[x, y] == 1) { sol[x, y] = 1; return true; } if (IsSafe(maze, x, y)) { if (sol[x, y] == 1) return false; sol[x, y] = 1; if (SolveMazeUtil(maze, x + 1, y, sol)) return true; if (SolveMazeUtil(maze, x, y + 1, sol)) return true; sol[x, y] = 0; return false; } return false; } // Note: N is not defined anywhere in this snippet.
Attempts:
2 left
💡 Hint
Check if all variables used are declared and accessible.
✗ Incorrect
The variable N is used but not declared or passed, causing a compile-time error.
🧠 Conceptual
advancedWhy Backtracking is Used in Rat in a Maze Problem?
Why is backtracking the preferred technique to solve the Rat in a Maze problem?
Attempts:
2 left
💡 Hint
Think about how the algorithm explores paths and handles failures.
✗ Incorrect
Backtracking explores all paths and backtracks when a path is invalid, ensuring all possibilities are checked.
🚀 Application
expertModify Maze to Block Shortest Path
Given the original maze below, which modification blocks the shortest path forcing the rat to take a longer route?
DSA C#
int[,] maze = { {1, 0, 0, 0}, {1, 1, 0, 1}, {0, 1, 0, 0}, {1, 1, 1, 1} };
Attempts:
2 left
💡 Hint
Identify the shortest path and block a critical cell on it.
✗ Incorrect
Blocking maze[2,1] cuts the direct path forcing the rat to find a longer alternative route.
