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
Search on Grids and Graphs
00:00
5 left

Search on Grids and Graphs

MediumPython

Problem

A BrainOS robot represents a mapped floor area as a rectangular grid. Free cells are marked 0, and blocked cells such as walls or unavailable areas are marked 1. Given a start cell and a goal cell, return the shortest route the robot can follow using four-directional movement.

If multiple shortest routes exist, explore neighbors in this order: up, right, down, left. Return the first route produced by that ordering. If the goal cannot be reached, return an empty list.

Formal Specification

Implement navigate_grid(grid, start, goal).

  • grid is a non-empty rectangular list of lists containing integers 0 or 1.
  • start and goal are two-element lists [row, column] identifying free cells.
  • The robot may move to an adjacent cell vertically or horizontally, but not diagonally or through blocked cells.
  • Return a list of coordinate lists beginning with start and ending with goal, or [] when no route exists.

Constraints

  • 1 <= rows, columns <= 200
  • grid is rectangular and contains only 0 and 1
  • start and goal are coordinates of free cells
  • Movement is limited to four cardinal directions
  • The returned route must include both endpoints when one exists

Function Signature

def navigate_grid(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.