Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the rat can move to the next cell inside the maze boundaries.
DSA C++
bool isSafe(int x, int y, int n) {
return (x >= 0 && y >= 0 && x < [1] && y < n);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using n-1 instead of n in the boundary check.
Not checking for negative indices.
✗ Incorrect
The maze size is n x n, so valid indices are from 0 to n-1. We check if x and y are less than n to stay inside boundaries.
2fill in blank
mediumComplete the code to mark the current cell as part of the solution path.
DSA C++
void solveMazeUtil(int maze[][[1]], int x, int y, int n, int sol[][[1]]) { sol[x][y] = 1; // rest of the code }
Attempts:
3 left
💡 Hint
Common Mistakes
Using x or y as the array size dimension.
Using a fixed size like 1.
✗ Incorrect
The maze and solution arrays are n x n, so the second dimension must be n.
3fill in blank
hardFix the error in the recursive call to move right in the maze.
DSA C++
if (solveMazeUtil(maze, x, y + 1, n, sol)) { return [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false instead of true.
Returning the maze or solution cell value.
✗ Incorrect
If the recursive call returns true, we return true to indicate the path is found.
4fill in blank
hardFill both blanks to check if the next cell is safe and open for the rat to move.
DSA C++
if (isSafe(x + [1], y + [2], n) && maze[x + [1]][y + [2]] == 1) { if (solveMazeUtil(maze, x + [1], y + [2], n, sol)) { return true; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 for x increment or 0 for y increment.
Using negative increments incorrectly.
✗ Incorrect
To move right, keep x the same (0) and increase y by 1.
5fill in blank
hardFill all three blanks to backtrack by unmarking the current cell and return false.
DSA C++
sol[x][y] = [1]; return [2]; // End of function returns [3] if no path found
Attempts:
3 left
💡 Hint
Common Mistakes
Setting solution cell to 1 instead of 0.
Returning true instead of false.
✗ Incorrect
Backtracking means unmarking the cell (0) and returning false to indicate failure.
