Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

BFS Shortest Path in Dynamic Grid

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

Your question is BFS Shortest Path in Dynamic Grid. 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 Games open-world environment models the map as a rectangular grid. Some cells are permanent walls, while other cells become blocked according to a repeating schedule. Implement a time-aware breadth-first search that returns the shortest path from start to goal.

At time 0, the player is at start. During each minute, the player may move to one of the four adjacent cells or wait in place. The destination cell must be inside the grid, not a permanent wall, and not dynamically blocked at the time of arrival. Dynamic blocking schedules repeat after len(dynamic_blocks) phases.

Formal Specification

Implement shortest_dynamic_path(grid, start, goal, dynamic_blocks).

  • grid is a non-empty list of equal-length strings. '.' is traversable and '#' is a permanent wall.
  • start and goal are integer coordinate lists [row, column].
  • dynamic_blocks is a non-empty list of phases. Each phase is a list of coordinate lists blocked during that phase.
  • Return a list of coordinate lists representing the shortest path, including start and goal. Return [] if no path exists.

A state is identified by its position and time phase, because the same position may be safe at one time and blocked at another.

Constraints

  • 1 <= rows, columns <= 100
  • 1 <= rows * columns <= 10^4
  • 1 <= len(dynamic_blocks) <= 20
  • Each phase contains at most rows * columns coordinates
  • Coordinates are valid and contain no duplicates within a phase
  • The grid contains only '.' and '#'

Function Signature

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