Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Graph Shortest Path

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

Your question is Graph Shortest Path. 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

A navigation system for a Rockstar game level represents a rectangular map as a grid. Find the shortest route from S, the starting position, to G, the goal, using only four-directional movement: up, down, left, and right. Cells marked # are blocked, while S, G, and . are walkable. Return every coordinate on the route, including both endpoints. If no route exists, return an empty list.

Formal Specification

Implement shortest_path(grid), where grid is a non-empty list of equal-length strings. Return a list of [row, column] coordinate pairs in traversal order. The route may be any shortest route if multiple shortest routes exist.

Constraints

  • 1 <= len(grid) <= 500
  • 1 <= len(grid[0]) <= 500
  • All rows have the same length
  • The grid contains exactly one S and exactly one G
  • The grid contains only S, G, ., and #

Function Signature

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