Bird
Raised Fist0
DSA C++programming~20 mins

Rat in a Maze Problem in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rat Maze Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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 C++
int maze[4][4] = {
  {1, 0, 0, 0},
  {1, 1, 0, 1},
  {0, 1, 0, 0},
  {1, 1, 1, 1}
};

bool solveMaze(int maze[4][4], int x, int y, int sol[4][4]) {
  if (x == 3 && y == 3) {
    sol[x][y] = 1;
    return true;
  }
  if (x >= 0 && y >= 0 && x < 4 && y < 4 && maze[x][y] == 1 && sol[x][y] == 0) {
    sol[x][y] = 1;
    if (solveMaze(maze, x + 1, y, sol)) return true;
    if (solveMaze(maze, x, y + 1, sol)) return true;
    sol[x][y] = 0;
    return false;
  }
  return false;
}

int main() {
  int sol[4][4] = {0};
  solveMaze(maze, 0, 0, sol);
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      std::cout << sol[i][j] << " ";
    }
    std::cout << std::endl;
  }
  return 0;
}
A[[1,0,0,0],[1,1,0,0],[0,1,0,0],[0,1,1,1]]
B[[1,0,0,0],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
C[[1,0,0,0],[1,1,0,1],[0,1,0,0],[1,1,1,1]]
D[[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
Attempts:
2 left
💡 Hint
Trace the path from start (0,0) to end (3,3) moving only right or down where maze cells are 1.
🧠 Conceptual
intermediate
1:30remaining
Understanding Backtracking in Rat in a Maze
Which statement best describes the role of backtracking in the Rat in a Maze problem?
ABacktracking marks all cells as part of the path regardless of obstacles.
BBacktracking moves only forward without revisiting any cell.
CBacktracking uses a queue to explore all paths simultaneously.
DBacktracking tries all possible paths and abandons a path when it hits a dead end.
Attempts:
2 left
💡 Hint
Think about what happens when the rat cannot move forward on a path.
🔧 Debug
advanced
1:30remaining
Identify the Error in Maze Boundary Check
What error will occur if the boundary check in the solveMaze function is changed to 'if (x > 0 && y > 0 && x < 4 && y < 4)'?
DSA C++
if (x > 0 && y > 0 && x < 4 && y < 4 && maze[x][y] == 1) {
  sol[x][y] = 1;
  // rest of code
}
ANo error; the code will run correctly.
BThe rat will move outside the maze boundaries causing a runtime error.
CThe rat will never start at (0,0) because the condition excludes zero indices.
DThe rat will mark all cells as part of the path regardless of obstacles.
Attempts:
2 left
💡 Hint
Check if the starting cell (0,0) satisfies the new condition.
❓ Predict Output
advanced
2:00remaining
Output of Maze with No Path
What is the output of the solution matrix if the maze has no path from start to end?
DSA C++
int maze[3][3] = {
  {1, 0, 0},
  {0, 0, 0},
  {0, 0, 1}
};

bool solveMaze(int maze[3][3], int x, int y, int sol[3][3]) {
  if (x == 2 && y == 2) {
    sol[x][y] = 1;
    return true;
  }
  if (x >= 0 && y >= 0 && x < 3 && y < 3 && maze[x][y] == 1 && sol[x][y] == 0) {
    sol[x][y] = 1;
    if (solveMaze(maze, x + 1, y, sol)) return true;
    if (solveMaze(maze, x, y + 1, sol)) return true;
    sol[x][y] = 0;
    return false;
  }
  return false;
}

int main() {
  int sol[3][3] = {0};
  solveMaze(maze, 0, 0, sol);
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      std::cout << sol[i][j] << " ";
    }
    std::cout << std::endl;
  }
  return 0;
}
A[[0,0,0],[0,0,0],[0,0,0]]
B[[1,0,0],[0,0,0],[0,0,1]]
C[[1,0,0],[0,0,0],[0,0,0]]
D[[1,1,1],[1,1,1],[1,1,1]]
Attempts:
2 left
💡 Hint
If no path exists, the solution matrix remains empty.
🚀 Application
expert
2:30remaining
Minimum Steps to Reach Destination in Maze
Given a maze represented by a 2D grid where 1 is path and 0 is blocked, which algorithm modification will find the minimum number of steps from start (0,0) to end (n-1,n-1)?
AUse Depth-First Search (DFS) with backtracking to explore all paths and pick the shortest.
BUse Breadth-First Search (BFS) instead of backtracking to find the shortest path.
CUse a greedy algorithm that always moves right or down to reach the end quickly.
DUse a recursive function that marks all reachable cells without tracking steps.
Attempts:
2 left
💡 Hint
Think about which search explores neighbors level by level.

Practice

(1/5)
1. What is the main goal of the Rat in a Maze Problem in Data Structures & Algorithms?
easy
A. Find a path from the start to the destination in a maze
B. Count the number of walls in the maze
C. Calculate the shortest distance between two points in a graph
D. Sort the maze cells in ascending order

Solution

  1. Step 1: Understand the problem context

    The Rat in a Maze problem involves navigating a maze represented by a grid to find a path from the start cell to the destination cell.
  2. Step 2: Identify the main objective

    The main objective is to find a valid path through open cells (usually marked as 1) from the start to the end point.
  3. Final Answer:

    Find a path from the start to the destination in a maze -> Option A
  4. Quick Check:

    Goal = Find path [OK]
Hint: Focus on pathfinding, not counting or sorting [OK]
Common Mistakes:
  • Confusing pathfinding with counting walls
  • Thinking it's about sorting cells
  • Assuming it's about shortest distance always
2. Which of the following is the correct way to declare a 2D maze array of size 4x4 in C++?
easy
A. int maze[4][4];
B. int maze(4,4);
C. int maze[4,4];
D. int maze{4,4};

Solution

  1. Step 1: Recall C++ array declaration syntax

    In C++, a 2D array is declared using square brackets for each dimension, like int maze[4][4];.
  2. Step 2: Analyze each option

    int maze[4][4]; uses correct syntax. int maze(4,4); uses parentheses which is invalid for arrays. int maze[4,4]; uses comma inside brackets which is incorrect. int maze{4,4}; uses braces which is invalid for declaration.
  3. Final Answer:

    int maze[4][4]; -> Option A
  4. Quick Check:

    2D array = int maze[4][4]; [OK]
Hint: Use square brackets for array dimensions [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Using commas inside brackets
  • Using braces for declaration
3. Consider the following C++ code snippet for a 2x2 maze where 1 means open path and 0 means blocked:
int maze[2][2] = {{1, 0}, {1, 1}};
bool solveMaze(int maze[2][2], int x, int y) {
  if (x == 1 && y == 1) return true;
  if (x < 0 || y < 0 || x >= 2 || y >= 2 || maze[x][y] == 0) return false;
  if (solveMaze(maze, x + 1, y)) return true;
  if (solveMaze(maze, x, y + 1)) return true;
  return false;
}
int main() {
  bool result = solveMaze(maze, 0, 0);
  std::cout << (result ? "Path Found" : "No Path") << std::endl;
  return 0;
}
What will be the output when this program runs?
medium
A. Runtime Error
B. No Path
C. Compilation Error
D. Path Found

Solution

  1. Step 1: Trace the recursive calls starting at (0,0)

    At (0,0), maze[0][0] = 1 (open). Try moving down to (1,0): maze[1][0] = 1 (open). From (1,0), try moving down to (2,0) invalid, then right to (1,1): maze[1][1] = 1 (open) and is destination.
  2. Step 2: Confirm path existence

    Since (1,1) is reached, function returns true and prints "Path Found".
  3. Final Answer:

    Path Found -> Option D
  4. Quick Check:

    Valid path exists = Path Found [OK]
Hint: Follow recursion down and right moves [OK]
Common Mistakes:
  • Ignoring boundary checks
  • Assuming no path due to blocked cell
  • Confusing indices order
4. The following code snippet is intended to solve the Rat in a Maze problem but has a bug:
bool solveMaze(int maze[3][3], int x, int y) {
  if (x == 2 && y == 2) return true;
  if (x > 2 || y > 2 || maze[x][y] == 0) return false;
  if (solveMaze(maze, x + 1, y)) return true;
  if (solveMaze(maze, x, y + 1)) return true;
  return false;
}
What is the bug in this code?
medium
A. Missing check for negative indices
B. No check for maze boundaries less than zero
C. No check for visited cells causing infinite recursion
D. Incorrect base case coordinates

Solution

  1. Step 1: Analyze boundary and base case checks

    The code checks if x or y are greater than 2 and if the cell is blocked. It returns true if destination (2,2) is reached.
  2. Step 2: Identify missing visited check

    There is no mechanism to mark visited cells, so the recursion can revisit the same cells repeatedly, causing infinite recursion or stack overflow.
  3. Final Answer:

    No check for visited cells causing infinite recursion -> Option C
  4. Quick Check:

    Visited check missing = Infinite recursion [OK]
Hint: Always mark visited cells to avoid loops [OK]
Common Mistakes:
  • Ignoring visited cells check
  • Assuming boundary checks cover all cases
  • Confusing base case with boundary
5. Given a 4x4 maze represented as:
int maze[4][4] = {
  {1, 0, 0, 0},
  {1, 1, 0, 1},
  {0, 1, 0, 0},
  {1, 1, 1, 1}
};
Write a function to find one path from (0,0) to (3,3) moving only right or down. Which of the following describes the correct approach?
hard
A. Use depth-first search but only move left and up
B. Use backtracking to explore right and down moves, marking visited cells to avoid cycles
C. Use breadth-first search without marking visited cells
D. Use a greedy approach always moving right first without backtracking

Solution

  1. Step 1: Understand movement constraints and maze structure

    The rat can move only right or down. The maze has blocked cells (0) and open cells (1). We need to find a path from top-left to bottom-right.
  2. Step 2: Choose correct algorithm approach

    Backtracking explores all possible paths by moving right or down, marking visited cells to avoid revisiting and infinite loops. Greedy or BFS without visited checks can fail or loop.
  3. Final Answer:

    Use backtracking to explore right and down moves, marking visited cells to avoid cycles -> Option B
  4. Quick Check:

    Backtracking + visited = Correct approach [OK]
Hint: Backtrack with visited marks for safe pathfinding [OK]
Common Mistakes:
  • Using greedy without backtracking
  • Ignoring visited cells causing loops
  • Moving in wrong directions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard