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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
Grid BFS Minimum Steps
00:00
5 left

Grid BFS Minimum Steps

MediumPython

Problem

Flexport warehouse planning may need to route a worker from a shipment pickup location to a destination through a grid containing blocked cells. Given a grid and a maximum number of blocked cells that may be cleared, return the minimum number of moves needed to reach the destination.

A move goes one cell up, down, left, or right. Entering a blocked cell, represented by #, consumes one clearance. Open cells are represented by ., the start by S, and the destination by E. Return -1 if the destination cannot be reached.

Formal Specification

Implement min_steps(grid, k):

  • grid is a list of equal-length strings containing exactly one S and one E.
  • k is a non-negative integer representing the maximum number of blocked cells that may be cleared.
  • Return an integer containing the minimum number of moves, or -1 when no valid route exists.
  • The same blocked cell may not be counted more than once during a single move, but routes may revisit cells if their clearance state differs.

Constraints

  • 1 <= rows, cols <= 200
  • 0 <= k <= 10
  • The grid contains exactly one S and one E
  • Each row has the same number of columns
  • The grid contains only S, E, ., and #

Function Signature

def min_steps(grid, k):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.