Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Find Shortest Path in a Grid Using BFS
00:00
5 left

Find Shortest Path in a Grid Using BFS

EasyPython

Problem

Given a 2D grid represented as a list of lists, where 0 represents an open cell and 1 represents a wall, implement a BFS algorithm to find the shortest path from the top-left corner (0, 0) to the bottom-right corner (n-1, m-1). If no path exists, return -1.

Constraints

  • 1 <= grid.length, grid[i].length <= 100
  • The grid will only contain 0s and 1s.

Function Signature

def shortest_path_bfs(grid):
Interviewer

Your question is Find Shortest Path in a Grid Using BFS. 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.