Challenge - 5 Problems
Rat 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 C++
int maze[4][4] = { {1, 0, 0, 0}, {1, 1, 0, 1}, {0, 1, 0, 0}, {1, 1, 1, 1} }; bool solveMaze(int maze[4][4], int x, int y, int sol[4][4]) { if (x == 3 && y == 3) { sol[x][y] = 1; return true; } if (x >= 0 && y >= 0 && x < 4 && y < 4 && maze[x][y] == 1 && sol[x][y] == 0) { sol[x][y] = 1; if (solveMaze(maze, x + 1, y, sol)) return true; if (solveMaze(maze, x, y + 1, sol)) return true; sol[x][y] = 0; return false; } return false; } int main() { int sol[4][4] = {0}; solveMaze(maze, 0, 0, sol); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { std::cout << sol[i][j] << " "; } std::cout << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
Trace the path from start (0,0) to end (3,3) 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), down to (3,1), then right to (3,2) and (3,3). The solution matrix marks these cells with 1.
🧠 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 on a path.
✗ Incorrect
Backtracking explores paths and if a path leads to no solution, it goes back (backtracks) to try other paths.
🔧 Debug
advancedIdentify the Error in Maze Boundary Check
What error will occur if the boundary check in the solveMaze function is changed to 'if (x > 0 && y > 0 && x < 4 && y < 4)'?
DSA C++
if (x > 0 && y > 0 && x < 4 && y < 4 && maze[x][y] == 1) { sol[x][y] = 1; // rest of code }
Attempts:
2 left
💡 Hint
Check if the starting cell (0,0) satisfies the new condition.
✗ Incorrect
The condition excludes cells where x or y is zero, so the rat cannot start at (0,0), making the maze unsolvable.
❓ Predict Output
advancedOutput of Maze with No Path
What is the output of the solution matrix if the maze has no path from start to end?
DSA C++
int maze[3][3] = { {1, 0, 0}, {0, 0, 0}, {0, 0, 1} }; bool solveMaze(int maze[3][3], int x, int y, int sol[3][3]) { if (x == 2 && y == 2) { sol[x][y] = 1; return true; } if (x >= 0 && y >= 0 && x < 3 && y < 3 && maze[x][y] == 1 && sol[x][y] == 0) { sol[x][y] = 1; if (solveMaze(maze, x + 1, y, sol)) return true; if (solveMaze(maze, x, y + 1, sol)) return true; sol[x][y] = 0; return false; } return false; } int main() { int sol[3][3] = {0}; solveMaze(maze, 0, 0, sol); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { std::cout << sol[i][j] << " "; } std::cout << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
If no path exists, the solution matrix remains empty.
✗ Incorrect
Since the rat cannot reach the destination, the solution matrix remains all zeros.
🚀 Application
expertMinimum Steps to Reach Destination in Maze
Given a maze represented by a 2D grid where 1 is path and 0 is blocked, which algorithm modification will find the minimum number of steps from start (0,0) to end (n-1,n-1)?
Attempts:
2 left
💡 Hint
Think about which search explores neighbors level by level.
✗ Incorrect
BFS explores all positions at the current distance before moving further, guaranteeing the shortest path is found first.
