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 Path in a Simple Maze
What is the output path of the rat in the maze after running the code below? The rat starts at top-left (0,0) and tries to reach bottom-right (2,2). 1 means open path, 0 means blocked.
DSA Python
def rat_in_maze(maze): n = len(maze) path = [[0]*n for _ in range(n)] def solve(x, y): if x == n-1 and y == n-1 and maze[x][y] == 1: path[x][y] = 1 return True if 0 <= x < n and 0 <= y < n and maze[x][y] == 1: if path[x][y] == 1: return False path[x][y] = 1 if solve(x+1, y): return True if solve(x, y+1): return True path[x][y] = 0 return False return False if solve(0, 0): return path else: return [] maze = [ [1, 0, 0], [1, 1, 0], [0, 1, 1] ] result = rat_in_maze(maze) print(result)
Attempts:
2 left
💡 Hint
Trace the path from top-left to bottom-right moving only down or right on cells with 1.
✗ Incorrect
The rat moves down from (0,0) to (1,0), then right to (1,1), then down to (2,1), and finally right to (2,2). The path matrix marks these cells with 1.
🧠 Conceptual
intermediateUnderstanding Rat Movement Constraints
In the Rat in a Maze problem, which of the following movement rules is typically allowed for the rat to find a path?
Attempts:
2 left
💡 Hint
Think about the simplest movement rules to keep the problem solvable.
✗ Incorrect
Typically, the rat moves only down or right to simplify the problem and avoid cycles.
🔧 Debug
advancedIdentify the Bug in Maze Pathfinding Code
What error will the following code produce when run? The maze is a 2x2 grid with all open paths.
DSA Python
def rat_in_maze(maze): n = len(maze) path = [[0]*n for _ in range(n)] def solve(x, y): if x == n-1 and y == n-1 and maze[x][y] == 1: path[x][y] = 1 return True if 0 <= x < n and 0 <= y < n and maze[x][y] == 1: if path[x][y] == 1: return False path[x][y] = 1 if solve(x+1, y): return True if solve(x, y+1): return True path[x][y] = 0 return False return False if solve(0, 0): return path else: return [] maze = [ [1, 1], [1, 1] ] result = rat_in_maze(maze) print(result)
Attempts:
2 left
💡 Hint
Check if the code prevents revisiting the same cell properly.
✗ Incorrect
The code does not prevent revisiting cells correctly, causing infinite recursion and stack overflow.
🚀 Application
advancedNumber of Paths in a Maze with Obstacles
Given a 3x3 maze with some blocked cells (0) and open cells (1), how many unique paths exist from top-left to bottom-right moving only down or right?
DSA Python
maze = [
[1, 1, 0],
[1, 1, 1],
[0, 1, 1]
]
# Count paths function not shownAttempts:
2 left
💡 Hint
Count all possible ways moving down or right avoiding blocked cells.
✗ Incorrect
There are exactly 2 unique paths avoiding blocked cells in this maze.
❓ Predict Output
expertOutput of Rat Path with Backtracking and Multiple Paths
What is the output path matrix after running the code below? The rat tries to find one path from top-left to bottom-right in a 4x4 maze.
DSA Python
def rat_in_maze(maze): n = len(maze) path = [[0]*n for _ in range(n)] def solve(x, y): if x == n-1 and y == n-1 and maze[x][y] == 1: path[x][y] = 1 return True if 0 <= x < n and 0 <= y < n and maze[x][y] == 1: if path[x][y] == 1: return False path[x][y] = 1 if solve(x+1, y): return True if solve(x, y+1): return True if solve(x-1, y): return True if solve(x, y-1): return True path[x][y] = 0 return False return False if solve(0, 0): return path else: return [] maze = [ [1, 0, 0, 0], [1, 1, 0, 1], [0, 1, 0, 0], [1, 1, 1, 1] ] result = rat_in_maze(maze) print(result)
Attempts:
2 left
💡 Hint
The rat can move back and forth but must find a valid path marking visited cells.
✗ Incorrect
The rat finds a path going down, right, down, and then right to reach the end. The path matrix marks these cells with 1.
