What is the main goal in the Rat in a Maze problem?
To find a path from the start point (usually top-left) to the destination (usually bottom-right) in a maze represented by a grid, moving only through allowed cells.
Click to reveal answer
beginner
Which algorithmic technique is commonly used to solve the Rat in a Maze problem?
Backtracking, which tries possible paths and backtracks when a path leads to a dead end.
Click to reveal answer
beginner
In the Rat in a Maze problem, what does a cell value of 0 usually represent?
A blocked cell where the rat cannot move.
Click to reveal answer
intermediate
Why do we mark cells as visited during the Rat in a Maze solution?
To avoid revisiting the same cell and getting stuck in infinite loops.
Click to reveal answer
advanced
What is the time complexity of the Rat in a Maze problem using backtracking on an N x N grid?
O(2^(N^2)) in the worst case, because each cell can be either part of the path or not, leading to exponential possibilities.
Click to reveal answer
In the Rat in a Maze problem, which moves are typically allowed?
AOnly Right and Down
BUp, Down, Left, Right
CDiagonal moves only
DAny direction including diagonal
✗ Incorrect
The rat can move up, down, left, or right, but not diagonally.
What does backtracking do when it hits a dead end in the maze?
AMarks the cell as part of the path anyway
BStops and returns failure immediately
CTries a different path by going back to the previous cell
DSkips the cell and moves forward
✗ Incorrect
Backtracking goes back to the previous cell to try a different path.
What data structure is commonly used to keep track of the path in the Rat in a Maze problem?
AStack or recursion call stack
BQueue
CHash map
DLinked list
✗ Incorrect
Backtracking uses recursion which implicitly uses a call stack to keep track of the path.
If the maze is represented by a 2D array, what does a value of 1 usually mean?
ADestination point
BBlocked cell
CStart point
DOpen cell where the rat can move
✗ Incorrect
A value of 1 means the cell is open and the rat can move through it.
What is the base case in the recursive solution of the Rat in a Maze problem?
AWhen the rat reaches the destination cell
BWhen the rat hits a blocked cell
CWhen all cells are visited
DWhen the rat moves out of the maze
✗ Incorrect
The base case is when the rat reaches the destination cell successfully.
Explain how backtracking helps solve the Rat in a Maze problem.
Think about trying moves and undoing them if they don't lead to success.
You got /4 concepts.
Describe the role of the maze grid and what the values inside it represent in the Rat in a Maze problem.
Consider how the rat knows where it can move.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of the Rat in a Maze problem in data structures?
easy
A. Find a path from the start to the destination in a maze
B. Count the total number of cells in the maze
C. Calculate the shortest distance between two points in a graph
D. Sort the maze cells in ascending order
Solution
Step 1: Understand the problem goal
The Rat in a Maze problem asks to find a path from the start cell (usually top-left) to the destination cell (usually bottom-right) in a maze represented by a grid.
Step 2: Identify the correct goal
The goal is not counting cells, sorting, or shortest distance but finding a valid path through allowed cells.
Final Answer:
Find a path from the start to the destination in a maze -> Option A
Quick Check:
Goal = Find path [OK]
Hint: Focus on pathfinding, not counting or sorting [OK]
Common Mistakes:
Confusing pathfinding with counting cells
Thinking it finds shortest path always
Mixing it with sorting or graph distance problems
2. Which of the following Rust function signatures is correct for a recursive function solving the Rat in a Maze problem?
Start at (0,0) which is 1 (allowed). Try moving down to (1,0) which is 1, then (2,0) is 0 (blocked), so backtrack. Then try right from (1,0) to (1,1) which is 1.
Step 2: Continue path to destination
From (1,1), down to (2,1) is 1, then down to (3,1) is 1, right to (3,2) is 1, and right to (3,3) is 1 (destination). Path is [(0,0),(1,0),(1,1),(2,1),(3,1),(3,2),(3,3)].
Hint: Follow down then right moves recursively [OK]
Common Mistakes:
Assuming path goes right first instead of down
Not backtracking correctly on blocked cells
Confusing coordinates order in path
4. Identify the bug in this Rust code snippet for Rat in a Maze and select the fix:
fn solve_maze(maze: &Vec, 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 solve_maze(maze, x + 1, y, path) {
return true;
}
if solve_maze(maze, x, y + 1, path) {
return true;
}
path.pop();
}
false
}
medium
A. Change maze parameter to mutable reference
B. Remove path.pop() to keep all visited cells
C. Add boundary checks before recursive calls to avoid out-of-bounds
D. Return i32 instead of bool for success
Solution
Step 1: Analyze recursive calls for boundary safety
The recursive calls use x+1 and y+1 without checking if they exceed maze size, risking out-of-bounds panic.
Step 2: Fix by adding boundary checks before recursion
Adding checks like x + 1 < n and y + 1 < n before calling solve_maze prevents invalid indexing.
Final Answer:
Add boundary checks before recursive calls to avoid out-of-bounds -> Option C
Quick Check:
Boundary checks prevent panic [OK]
Hint: Always check indices before recursive calls [OK]
Common Mistakes:
Ignoring index bounds causing runtime panic
Changing maze to mutable unnecessarily
Removing path.pop() causing wrong paths
5. You want to modify the Rat in a Maze solution in Rust to find all possible paths from start to finish instead of just one. Which approach is best?
hard
A. Use a queue to perform breadth-first search and return the shortest path only
B. Return a vector of paths and explore all possible moves recursively without stopping at first success
C. Stop recursion as soon as one path is found to save time
D. Modify the maze to mark visited cells permanently to avoid revisiting
Solution
Step 1: Understand requirement to find all paths
Finding all paths means exploring every possible route without stopping at the first success.
Step 2: Use recursion to explore all moves and collect paths
Return a vector of paths and do not return early. Backtrack properly to explore all options.
Final Answer:
Return a vector of paths and explore all possible moves recursively without stopping at first success -> Option B
Quick Check:
Explore all paths recursively [OK]
Hint: Collect all paths, don't stop at first success [OK]
Common Mistakes:
Stopping recursion early losing other paths
Using BFS only for shortest path, not all paths
Marking visited permanently blocking other paths
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.