PPractick

DSA interview question

Rat in a Maze: All Paths

Hard · Algorithms · Topics: recursion, backtracking, matrix

The problem

A robot vacuum maps a square room as an n × n grid: 1 is a free tile and 0 is furniture. It starts at the top-left tile and must reach the charging dock at the bottom-right, moving Down, Left, Right or Up one tile at a time and never revisiting a tile on the same trip. The planning screen should list every possible route as a string of moves (like DRDDRR), in lexicographic order. Backtracking explores moves in alphabetical order (D, L, R, U), marks tiles on the current path, and unmarks them when backing out.

Example: maze 1000 / 1101 / 1100 / 0111 → DDRDRR DRDDRR.

Function signature

vector<string> findPaths(const vector<vector<int>>& maze);  // sorted; empty if unreachable

Test cases

Input maze (rows)OutputExplanation
1000 / 1101 / 1100 / 0111DDRDRR DRDDRR
10 / 10(none)The destination is blocked
11 / 11DR RD
01 / 11(none)The start is blocked
111 / 101 / 111DDRR RRDDTwo ways around the centre

(Confirm: moves are D, L, R, U only; a tile can't be revisited on one path; if the start or destination is 0, return no paths.)

How interviewers phrase it

Clarifying questions to ask

  1. Which moves are allowed? (Down, Left, Right, Up.)
  2. Can a cell be revisited on the same path? (No.)
  3. Must the output be sorted?

Follow-ups to expect

  1. "How do you get sorted output without sorting?"
  2. "Why unmark on the way back?"
  3. "Complexity?"
  4. "Shortest path only?"
  5. "How can you avoid a separate visited array?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in