Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

BFS Shortest Path to Call

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

Your question is BFS Shortest Path to Call. 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

Axon Dispatch represents a service area as a rectangular grid. An officer must reach an emergency call using the shortest route through traversable cells while avoiding blocked cells.

Implement shortest_path(grid, start, target) using Breadth-First Search. Return the shortest route as a list of coordinate pairs, including both start and target. If the target cannot be reached, return an empty list.

Formal Specification

  • grid is a list of equal-length lists containing 0 for a traversable cell and 1 for a blocked cell.
  • start and target are coordinate lists in the form [row, column].
  • Movement is allowed only up, down, left, or right. Diagonal movement is not allowed.
  • The start and target cells are guaranteed to be traversable.
  • Return a list of coordinate lists representing any shortest path. If multiple shortest paths exist, any one is valid.

Constraints

  • 1 <= len(grid), len(grid[0]) <= 500
  • Every row has the same length
  • grid contains only 0 and 1
  • start and target are traversable cells
  • Movement is limited to four cardinal directions

Function Signature

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