Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Shortest Path in a Grid Using BFS

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

Your question is Find Shortest Path in a Grid Using BFS. 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

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output