coding
Bloomberg
Google
Amazon

Bloomberg Coding: Grid Shortest Path with K Breaks

Topics:
Breadth-First Search
Graph Theory
Dynamic Programming
Roles:
Software Engineer
Backend Engineer
SWE
Experience:
Entry Level
Mid Level
Senior

Question Description

You are given an m x n grid of 0s (open) and 1s (walls). Starting at (0,0) and moving in four directions, compute the minimum number of steps to reach (m-1,n-1) if you may remove up to k walls along the path. Traversing a 0 costs no wall removals; traversing a 1 consumes one removal. Return the step count or -1 if unreachable.

Approach and flow you should present in an interview:

  • Start with a BFS that tracks state as (x, y, remaining_k, steps). Each move increments steps; entering a 1 decrements remaining_k. Use a visited structure keyed by (x,y) that stores the maximum remaining_k seen at that cell to prune worse states.

  • Early exits & optimizations: if k >= (m-1)+(n-1) you can return the Manhattan distance immediately. Consider using A* with a Manhattan heuristic or pruning when remaining_k is worse than a previously seen value for the same cell.

  • Complexity and trade-offs: the canonical solution has O(m * n * k) time and similar space in the worst case. Explain memory/time trade-offs if you try to compress state or apply heuristics.

Skill signals interviewers look for: correct BFS-with-state implementation, proper visited/pruning to avoid TLE, handling edge cases (single-cell grid, k=0), complexity analysis, and thoughtful optimizations (A*, heuristic pruning).

Common Follow-up Questions

  • How would you change the algorithm if diagonal moves were allowed (8 directions)?
  • Modify the objective to first minimize walls broken, then among those paths minimize steps. How do you model and implement that?
  • If each wall had a weight (cost) and you have a total removal budget, how would you adapt the solution (weighted obstacles)?
  • How can you reduce memory usage for very large grids? Can you compress the visited state or use bidirectional search?
  • Explain how A* with a Manhattan heuristic would change runtime in practice and whether it preserves correctness here.

Related Questions

1Shortest Path in Binary Matrix (reach end in a grid of 0/1 without removals)
2Minimum Obstacles to Remove to Reach Corner (minimize walls removed instead of steps)
30-1 BFS and Dijkstra variants on grid with weighted edges
4Find number of distinct shortest paths in a grid with limited obstacle removals

Explore More Questions

Practice This Question with AI

Get real-time hints, detailed requirements, and insightful analysis of the question.

Bloomberg Coding: Grid Shortest Path with K Wall Breaks | Voker