Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Grid Routing With Dijkstra or A*
00:00
5 left

Grid Routing With Dijkstra or A*

MediumPython

Problem

A Dataiku DSS plugin represents a routing surface as a rectangular grid. Each cell contains -1 for an obstacle or a positive traversal cost. Given a start and goal cell, return an optimal route that minimizes the sum of costs for entered cells.

You may move up, down, left, or right. Diagonal movement is not allowed. The start cell contributes zero cost because the route begins there. Return an ordered list of [row, column] coordinates, including both endpoints. If the goal is unreachable, return an empty list.

Implement optimal_route(grid, start, goal) using Dijkstra's algorithm. An A* implementation with an admissible heuristic is an acceptable alternative.

Formal Specification

  • grid is a non-empty rectangular list of lists of integers.
  • start and goal are two-element coordinate lists [row, column].
  • A traversable cell has a cost from 1 through 9; -1 represents an obstacle.
  • Return a list of coordinate lists describing a minimum-cost route, or [] when no route exists.
  • If multiple minimum-cost routes exist, return any one of them.

Constraints

  • 1 <= rows, columns <= 500
  • -1 <= grid[row][column] <= 9
  • Traversable cells have costs from 1 through 9
  • start and goal are traversable coordinates
  • Movement is limited to four orthogonal directions

Function Signature

def optimal_route(grid, start, goal):
Interviewer

Your question is Grid Routing With Dijkstra or A*. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.