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 of the rat in the maze after running the given Rust code?
DSA Rust
fn rat_in_maze(maze: &Vec<Vec<i32>>, x: usize, y: usize, path: &mut Vec<(usize, usize)>) -> bool { let n = maze.len(); if x == n - 1 && y == n - 1 { path.push((x, y)); return true; } if x < n && y < n && maze[x][y] == 1 { path.push((x, y)); if rat_in_maze(maze, x + 1, y, path) { return true; } if rat_in_maze(maze, x, y + 1, path) { return true; } path.pop(); } false } fn main() { let maze = vec![ vec![1, 0, 0, 0], vec![1, 1, 0, 1], vec![0, 1, 0, 0], vec![1, 1, 1, 1], ]; let mut path = Vec::new(); if rat_in_maze(&maze, 0, 0, &mut path) { for (x, y) in path { print!("({},{}) ", x, y); } } else { println!("No path found"); } }
Attempts:
2 left
💡 Hint
Trace the path by moving down first, then right, only through cells with value 1.
✗ Incorrect
The rat starts at (0,0). It moves down to (1,0), then right to (1,1), down to (2,1), down to (3,1), right to (3,2), and finally right to (3,3). This is the only valid path through cells with value 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 how the algorithm handles dead ends.
✗ Incorrect
Backtracking explores paths and if a path leads to a dead end, it goes back (undoes moves) to try other paths until it finds the exit.
🔧 Debug
advancedIdentify the Error in Maze Boundary Check
What error will occur when running this Rust code for the Rat in a Maze problem?
DSA Rust
fn rat_in_maze(maze: &Vec<Vec<i32>>, x: usize, y: usize, path: &mut Vec<(usize, usize)>) -> bool { let n = maze.len(); if x == n - 1 && y == n - 1 { path.push((x, y)); return true; } if maze[x][y] == 1 { path.push((x, y)); if rat_in_maze(maze, x + 1, y, path) { return true; } if rat_in_maze(maze, x, y + 1, path) { return true; } path.pop(); } false } fn main() { let maze = vec![ vec![1, 0, 0], vec![1, 1, 0], vec![0, 1, 1], ]; let mut path = Vec::new(); if rat_in_maze(&maze, 0, 0, &mut path) { for (x, y) in path { print!("({},{}) ", x, y); } } else { println!("No path found"); } }
Attempts:
2 left
💡 Hint
Check if the code verifies x and y are within maze size before accessing maze[x][y].
✗ Incorrect
The code accesses maze[x][y] without checking if x and y are within bounds, which can cause a runtime panic if x or y exceed maze size.
🚀 Application
advancedModify Maze to Block a Path
Given the maze below, which modification will block the only path from start (0,0) to end (3,3)?
DSA Rust
let maze = vec![
vec![1, 0, 0, 0],
vec![1, 1, 0, 1],
vec![0, 1, 0, 0],
vec![1, 1, 1, 1],
];Attempts:
2 left
💡 Hint
Block a cell that is part of the only valid path.
✗ Incorrect
Changing maze[2][1] to 0 blocks the path at a critical point, making it impossible for the rat to reach the end.
❓ Predict Output
expertOutput of Rat in Maze with Multiple Paths
What is the output path of the rat in the maze after running the given Rust code that explores all paths and returns the first found?
DSA Rust
fn rat_in_maze(maze: &Vec<Vec<i32>>, x: usize, y: usize, path: &mut Vec<(usize, usize)>) -> bool { let n = maze.len(); if x == n - 1 && y == n - 1 { path.push((x, y)); return true; } if x < n && y < n && maze[x][y] == 1 { path.push((x, y)); if rat_in_maze(maze, x, y + 1, path) { return true; } if rat_in_maze(maze, x + 1, y, path) { return true; } path.pop(); } false } fn main() { let maze = vec![ vec![1, 1, 0, 0], vec![0, 1, 1, 1], vec![0, 0, 0, 1], vec![0, 0, 0, 1], ]; let mut path = Vec::new(); if rat_in_maze(&maze, 0, 0, &mut path) { for (x, y) in path { print!("({},{}) ", x, y); } } else { println!("No path found"); } }
Attempts:
2 left
💡 Hint
The code tries moving right first, then down.
✗ Incorrect
The rat moves right first from (0,0) to (0,1), then down to (1,1), right to (1,2), right to (1,3), down to (2,3), and down to (3,3).
