Robot Maze Coding Interview Question | Skilled.dev
Interview Question

Robot Maze

You are training a robot to walk through a room to search for an exit. The room will be represented as a matrix. The robot will always start at the top-left at point (0, 0) and can only walk down or right.

There may or may not be a path to the exit in the room. An open node is represented by 0, a blocked node is represented by 1, and an exit is represented by 2.

Write a function hasExitPath that returns a boolean if there is a path to an exit or not.

hasExitPath([
  [0, 0, 0],
  [0, 0, 1],
  [0, 0, 1],
  [1, 0, 2],
]); // true

hasExitPath([
  [0, 0, 0, 0],
  [1, 0, 0, 1],
  [2, 1, 0, 0],
]); // false

Breakdown

Loading...

Table of Contents

Validate My Answer

  1. There can be many paths. We only care if at least one reaches the exit, and then we can return true.

  2. If you solve this recursively, you may be performing a lot of duplicate work. Try to remove these repeated operations to improve your time complexity.

  3. You can solve this in O(r x c) time and space, where r is the number of rows and c is the number of columns.

Loading...

Table of Contents

Test Results

Run your code and see results here...

function hasExitPath(roomMatrix) {
  // Your solution here
};
// Upgrade for full course access
// Upgrade for full course access

Robot Maze

You are training a robot to walk through a room to search for an exit. The room will be represented as a matrix. The robot will always start at the top-left at point (0, 0) and can only walk down or right.

There may or may not be a path to the exit in the room. An open node is represented by 0, a blocked node is represented by 1, and an exit is represented by 2.

Write a function hasExitPath that returns a boolean if there is a path to an exit or not.

hasExitPath([
  [0, 0, 0],
  [0, 0, 1],
  [0, 0, 1],
  [1, 0, 2],
]); // true

hasExitPath([
  [0, 0, 0, 0],
  [1, 0, 0, 1],
  [2, 1, 0, 0],
]); // false
function hasExitPath(roomMatrix) {
  // Your solution here
};
// Upgrade for full course access
// Upgrade for full course access