Challenge - 5 Problems
Maze Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Rat Path in a 4x4 Maze
What is the output path of the rat in the maze after running the given code? The rat starts at (0,0) and can move only right or down through cells with value 1.
DSA C
int maze[4][4] = { {1, 0, 0, 0}, {1, 1, 0, 1}, {0, 1, 0, 0}, {1, 1, 1, 1} }; int solution[4][4] = {0}; int solveMaze(int maze[4][4], int x, int y, int solution[4][4]) { if (x == 3 && y == 3 && maze[x][y] == 1) { solution[x][y] = 1; return 1; } if (x >= 0 && y >= 0 && x < 4 && y < 4 && maze[x][y] == 1) { solution[x][y] = 1; if (solveMaze(maze, x + 1, y, solution)) return 1; if (solveMaze(maze, x, y + 1, solution)) return 1; solution[x][y] = 0; return 0; } return 0; } int main() { if (solveMaze(maze, 0, 0, solution)) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { printf("%d ", solution[i][j]); } printf("\n"); } } else { printf("No path found\n"); } return 0; }
Attempts:
2 left
💡 Hint
Trace the path by moving only right or down through cells with value 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
intermediate1:30remaining
Understanding Rat Movement Constraints
In the Rat in a Maze problem, if the rat can move only right and down, which of the following statements is true about the possible paths?
Attempts:
2 left
💡 Hint
Consider the allowed moves and the maze layout.
✗ Incorrect
The rat can only move right or down through cells with value 1, so it can only reach cells connected by such paths.
🔧 Debug
advanced2:00remaining
Identify the Bug in Maze Solver Code
What error will occur when running this code snippet for the Rat in a Maze problem?
DSA C
int solveMaze(int maze[4][4], int x, int y, int solution[4][4]) { if (x == 3 && y == 3 && maze[x][y] == 1) { solution[x][y] = 1; return 1; } if (x >= 0 && y >= 0 && x < 4 && y < 4 && maze[x][y] == 1) { solution[x][y] = 1; if (solveMaze(maze, x + 1, y, solution)) return 1; if (solveMaze(maze, x, y + 1, solution)) return 1; solution[x][y] = 0; return 0; } return 0; }
Attempts:
2 left
💡 Hint
Check if the code backtracks properly when a path is invalid.
✗ Incorrect
The code does not reset solution[x][y] to 0 when backtracking, causing infinite recursion and stack overflow.
🚀 Application
advanced1:30remaining
Number of Paths in a Maze with Obstacles
Given a 3x3 maze where 1 represents open path and 0 represents obstacle:
int maze[3][3] = {
{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 right or down?
Attempts:
2 left
💡 Hint
Count all paths moving right/down avoiding zeros.
✗ Incorrect
There are three unique paths avoiding obstacles: (0,0)->(0,1)->(1,1)->(1,2)->(2,2), (0,0)->(1,0)->(1,1)->(1,2)->(2,2), and (0,0)->(1,0)->(1,1)->(2,1)->(2,2).
🧠 Conceptual
expert2:00remaining
Time Complexity of Rat in a Maze Backtracking Algorithm
What is the worst-case time complexity of the backtracking solution for the Rat in a Maze problem on an N x N maze?
Attempts:
2 left
💡 Hint
Consider that each cell can be either part of the path or not, and the rat can move in two directions.
✗ Incorrect
In the worst case, the rat explores all possible paths through the maze, which can be exponential in the number of cells, i.e., O(2^(N^2)).