Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Grid Traversal Function

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Grid Traversal Function. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

In a Google DeepMind MuJoCo navigation simulation, an agent moves through a rectangular grid. Cells with value 0 are traversable, while cells with value 1 contain obstacles. Given the grid, a start coordinate, and a goal coordinate, return the minimum number of moves required to reach the goal. The agent may move up, down, left, or right, but not diagonally or through obstacles.

Return -1 if the goal cannot be reached. The start and goal cells are guaranteed to be traversable.

Formal Specification

Implement shortest_path(grid, start, goal), where grid is a non-empty rectangular list of lists containing integers 0 or 1, and start and goal are coordinates represented as two-element lists [row, column]. Return an integer number of moves.

Constraints

  • 1 <= rows, columns <= 500
  • grid is rectangular
  • Each cell is either 0 or 1
  • start and goal are valid traversable coordinates
  • A move changes exactly one coordinate by 1 or -1

Function Signature

def shortest_path(grid, start, goal):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output