Dev.to
6/19/2026

Rat in a Maze | Backtracking
Short summary
Rat in a Maze is a classic backtracking interview problem where you find all valid paths from (0,0) to (n-1, n-1) in an n×n grid, using DFS with a visited matrix to prevent cycles. The algorithm marks cells visited, explores all four directions recursively, then unmarks to allow reuse in other paths—achieving O(4^N²) complexity. This pattern generalizes to Sudoku, N-Queens, Word Search, Palindrome Partitioning, Permutations, and Graph Coloring, making it essential for technical interviews.
- •Find all valid paths in a grid from start to end using backtracking and DFS
- •Mark cells as visited during exploration; unmark after to allow reuse in other paths
- •Generalizes to Sudoku, N-Queens, Word Search, and other classic interview problems
Generated with AI, which can make mistakes.
Is this a good recommendation for you?



