Rat in a Maze Problem Statement

You need to determine all possible paths for a rat starting at position (0, 0) in a square maze to reach its destination at (N-1, N-1). The maze is represented as an N*N matrix where a cell with value 0 is blocked and value 1 is open for passage. The rat may move in the directions 'U' (up), 'D' (down), 'L' (left), and 'R' (right).

Input:

N (integer representing matrix dimensions)
N lines of N space-separated integers (0 or 1)

Output:

A list of strings, each representing a valid path from source to destination sorted in alphabetical order.
Each path is in the form of a string with directions ('U', 'D', 'L', 'R').

Example:

Input:
4
1 0 0 0
1 1 0 0
1 1 0 0
0 1 1 1

Output:
['DDRDRR', 'DRDDRR']

Constraints:

  • 2 <= N <= 5
  • 0 <= MATRIX[i][j] <= 1

Note:

Implement the function to find paths, no need to handle output printing as it's managed elsewhere.

AnswerBot
4mo

Find all possible paths for a rat in a maze from source to destination.

  • Use backtracking to explore all possible paths in the maze.

  • Keep track of visited cells to avoid revisiting them.

  • Recursively try m...read more

Help your peers!
Select
Add answer anonymously...

JUSPAY Software Developer Intern interview questions & answers

A Software Developer Intern was asked 12mo agoQ. How is a set implemented?
A Software Developer Intern was asked 12mo agoQ. How is an unordered set implemented?
A Software Developer Intern was asked Q. Largest Cycle in Maze Problem Statement Given a maze represented by 'N' cells nu...read more
JUSPAY Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits