Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the rat can move to the next cell safely.
DSA Typescript
function isSafe(maze: number[][], x: number, y: number): boolean {
return x >= 0 && y >= 0 && x < maze.length && y < maze[0].length && maze[x][y] [1] 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes wrong path checks.
Not checking boundaries leads to errors.
✗ Incorrect
The rat can move to the cell only if the cell value is 1 (open path). So we check if maze[x][y] == 1.
2fill in blank
mediumComplete the code to mark the current cell as part of the solution path.
DSA Typescript
function solveMazeUtil(maze: number[][], x: number, y: number, sol: number[][]): boolean {
if (x === maze.length - 1 && y === maze[0].length - 1) {
sol[x][y] = [1];
return true;
}
if (isSafe(maze, x, y)) {
sol[x][y] = 1;
// rest of code omitted
}
return false;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Marking with 0 instead of 1 causes no path to be recorded.
Using negative values is incorrect.
✗ Incorrect
Marking sol[x][y] as 1 means this cell is part of the solution path.
3fill in blank
hardFix the error in the recursive call to move down in the maze.
DSA Typescript
if (solveMazeUtil(maze, x [1], y, sol)) { return true; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '- 1' moves up, not down.
Using '* 1' or '/ 1' does not change position.
✗ Incorrect
To move down, increase the x coordinate by 1 (x + 1).
4fill in blank
hardFill both blanks to move right and backtrack correctly.
DSA Typescript
if (solveMazeUtil(maze, x, y [1], sol)) { return true; } sol[x][y] = [2]; // backtracking
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '- 1' moves left, not right.
Setting backtracking to 1 keeps the cell marked.
✗ Incorrect
To move right, increase y by 1. For backtracking, reset sol[x][y] to 0.
5fill in blank
hardFill all three blanks to complete the main function that initializes and starts the maze solving.
DSA Typescript
function solveMaze(maze: number[][]): number[][] | null {
const sol = Array(maze.length).fill(null).map(() => Array(maze[0].length).fill([1]));
if (solveMazeUtil(maze, [2], [3], sol)) {
return sol;
}
return null;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing solution matrix with 1 causes wrong path.
Starting from other than (0, 0) misses the maze start.
✗ Incorrect
Initialize solution matrix with 0s. Start solving from position (0, 0).