Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
BFS Shortest Path in Dynamic Grid
00:00
5 left

BFS Shortest Path in Dynamic Grid

HardPython

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):
Interviewer

Your question is BFS Shortest Path in Dynamic Grid. 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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.