Challenge - 5 Problems
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 code?
DSA Go
package main import "fmt" func solveMaze(maze [][]int, x, y int, path *[][]int) bool { n := len(maze) if x == n-1 && y == n-1 { *path = append(*path, []int{x, y}) return true } if x >= 0 && y >= 0 && x < n && y < n && maze[x][y] == 1 { maze[x][y] = 0 *path = append(*path, []int{x, y}) if solveMaze(maze, x+1, y, path) { return true } if solveMaze(maze, x, y+1, path) { return true } *path = (*path)[:len(*path)-1] maze[x][y] = 1 } return false } func main() { maze := [][]int{ {1, 0, 0, 0}, {1, 1, 0, 1}, {0, 1, 0, 0}, {1, 1, 1, 1}, } path := [][]int{} if solveMaze(maze, 0, 0, &path) { fmt.Println(path) } else { fmt.Println("No path found") } }
Attempts:
2 left
💡 Hint
Trace the path by moving down first, then right, marking visited cells.
✗ Incorrect
The rat moves down from (0,0) to (1,0), then right to (1,1), down to (2,1), then down to (3,1), right to (3,2), and finally right to (3,3). This is the valid path found by the recursive function.
🧠 Conceptual
intermediateUnderstanding the Maze Representation
In the Rat in a Maze problem, what does the value '1' in the maze matrix represent?
Attempts:
2 left
💡 Hint
Think about which cells allow movement.
✗ Incorrect
In the maze matrix, '1' represents an open cell where the rat can move. '0' represents blocked cells.
🔧 Debug
advancedIdentify the Bug in Maze Solver
What error will occur if the line 'maze[x][y] = 0' is removed from the solveMaze function?
DSA Go
func solveMaze(maze [][]int, x, y int, path *[][]int) bool { n := len(maze) if x == n-1 && y == n-1 { *path = append(*path, []int{x, y}) return true } if x >= 0 && y >= 0 && x < n && y < n && maze[x][y] == 1 { // maze[x][y] = 0 // This line is removed *path = append(*path, []int{x, y}) if solveMaze(maze, x+1, y, path) { return true } if solveMaze(maze, x, y+1, path) { return true } *path = (*path)[:len(*path)-1] // maze[x][y] = 1 } return false }
Attempts:
2 left
💡 Hint
Consider what happens if the rat revisits the same cell repeatedly.
✗ Incorrect
Without marking the current cell as visited (setting it to 0), the rat can keep revisiting the same cell causing infinite recursion.
🚀 Application
advancedNumber of Paths in a Maze
Given a 2x2 maze where all cells are open (value 1), how many unique paths exist from top-left to bottom-right if the rat can only move right or down?
DSA Go
maze := [][]int{ {1, 1}, {1, 1}, }
Attempts:
2 left
💡 Hint
Count all possible ways moving only right or down in a 2x2 grid.
✗ Incorrect
The rat can move right then down, or down then right, so there are 2 unique paths.
❓ Predict Output
expertOutput of Maze Solver with No Possible Path
What is the output of the following code when the maze has no path from start to end?
DSA Go
package main import "fmt" func solveMaze(maze [][]int, x, y int, path *[][]int) bool { n := len(maze) if x == n-1 && y == n-1 { *path = append(*path, []int{x, y}) return true } if x >= 0 && y >= 0 && x < n && y < n && maze[x][y] == 1 { maze[x][y] = 0 *path = append(*path, []int{x, y}) if solveMaze(maze, x+1, y, path) { return true } if solveMaze(maze, x, y+1, path) { return true } *path = (*path)[:len(*path)-1] maze[x][y] = 1 } return false } func main() { maze := [][]int{ {1, 0}, {0, 1}, } path := [][]int{} if solveMaze(maze, 0, 0, &path) { fmt.Println(path) } else { fmt.Println("No path found") } }
Attempts:
2 left
💡 Hint
Check if the rat can move to any adjacent cell from the start.
✗ Incorrect
The maze blocks all paths from start to end, so the function returns false and prints 'No path found'.
