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's Path in a Simple Maze
What is the output path printed by the following Java code for the Rat in a Maze problem?
DSA Java
public class Maze { static int N = 4; static boolean isSafe(int x, int y, int[][] maze) { return (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1); } static boolean 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(x, y, maze)) { 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; } static void solveMaze(int[][] maze) { int[][] sol = new int[N][N]; if (!solveMazeUtil(maze, 0, 0, sol)) { System.out.println("No solution"); return; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { System.out.print(sol[i][j] + " "); } System.out.println(); } } public static void main(String[] args) { 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 starting from (0,0) moving only right or down where maze cells are 1.
✗ Incorrect
The rat moves down from (0,0) to (1,0), then right to (1,1), down to (2,1), and finally down and right to reach (3,3). The solution matrix marks these cells with 1.
🧠 Conceptual
intermediateUnderstanding the Movement Constraints
In the Rat in a Maze problem, which of the following movement rules is applied in the provided Java solution?
Attempts:
2 left
💡 Hint
Look at the recursive calls in solveMazeUtil method.
✗ Incorrect
The code tries to move down (x+1, y) first, then right (x, y+1), so only these two directions are allowed.
🔧 Debug
advancedIdentify the Error in Maze Solution Code
What error will occur if the line 'if (sol[x][y] == 1) return false;' is removed from the solveMazeUtil method?
DSA Java
static boolean 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(x, y, maze)) { // Removed line: 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; }
Attempts:
2 left
💡 Hint
Consider what happens if the rat revisits the same cell repeatedly.
✗ Incorrect
Without checking if the cell is already part of the solution path, the recursion can cycle infinitely causing stack overflow.
❓ Predict Output
advancedOutput for Maze with No Possible Path
What will be the output of the following Java code when the maze has no path from start to end?
DSA Java
public class Maze { static int N = 3; static boolean isSafe(int x, int y, int[][] maze) { return (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1); } static boolean 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(x, y, maze)) { 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; } static void solveMaze(int[][] maze) { int[][] sol = new int[N][N]; if (!solveMazeUtil(maze, 0, 0, sol)) { System.out.println("No solution"); return; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { System.out.print(sol[i][j] + " "); } System.out.println(); } } public static void main(String[] args) { int[][] maze = { {1, 0, 0}, {0, 0, 0}, {0, 0, 1} }; solveMaze(maze); } }
Attempts:
2 left
💡 Hint
Check if the rat can reach the bottom-right corner with given maze.
✗ Incorrect
Since no path exists, the code prints "No solution".
🚀 Application
expertNumber of Unique Paths in a Maze
Given a maze represented by a 2D array where 1 means open cell and 0 means blocked, which approach correctly counts the total number of unique paths from top-left to bottom-right moving only right or down?
Attempts:
2 left
💡 Hint
Think about counting all possible paths, not just one.
✗ Incorrect
Recursion with memoization efficiently counts all unique paths by avoiding repeated calculations.
