This is a premium problem. We're working on making it available for free soon.
Explore Free ProblemsSolutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, variations of maze or rolling-ball shortest path problems appear in interviews at large tech companies. They test graph traversal, shortest-path algorithms, and the ability to adapt BFS concepts to weighted scenarios.
The optimal approach is to use Dijkstra’s algorithm with a priority queue. Because the ball rolls until hitting a wall, the distance between nodes varies, making it a weighted shortest-path problem rather than a simple BFS traversal.
A priority queue (min-heap) is commonly used to implement Dijkstra’s algorithm. It allows the algorithm to always process the position with the smallest current distance, ensuring efficient shortest-path computation.
A standard BFS assumes equal edge weights, but in this problem the ball may travel different distances before stopping. Since each move can have a different cost, Dijkstra’s algorithm is more appropriate for ensuring the shortest path.