Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Compute Shortest Path Through Obstacles
00:00
5 left

Compute Shortest Path Through Obstacles

EasyPython

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):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.