← Bytedance Interview Insights
My first instinct was plain BFS and I started coding it before catching myself.
Model the problem as a shortest path on a state graph where each state is (row, col, obstacles_removed). Use BFS since each move costs 1 step, and track the minimum obstacles removed to reach each cell to prune dominated states. Return the distance when reaching the bottom-right with obstacles_removed ≤ k, else -1.
Pro tip: Emphasize that BFS guarantees the shortest path in unweighted graphs, and mention that you can optimize by storing the minimum obstacles removed per cell to avoid revisiting with worse states. This shows you understand both correctness and efficiency.
Confirm grid dimensions, movement directions (usually 4-directional), and that removing an obstacle counts toward k. Ask about edge cases like start or end being obstacles.
State: (r, c, removed). From (r, c), move to adjacent cells; if the neighbor is an obstacle, increment removed by 1. Only allow moves where removed ≤ k.
Use a queue for BFS. Maintain a 2D array minRemoved[r][c] to store the minimum obstacles removed to reach (r, c). Only enqueue a state if it improves minRemoved[r][c].
When popping a state at (m-1, n-1), return the current step count. If the queue empties without reaching the target, return -1.
Time: O(m*n*k) worst-case, but with pruning often O(m*n). Space: O(m*n). Discuss edge cases: k=0, start/end blocked, no path.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.