Can a Rolling Ball Stop on the Exit of a Tilt Maze

Last updated: November 29, 2026

Quick Overview

In a maze grid of 0 (open) and 1 (wall), a ball rolls in one of four directions and keeps going until it hits a wall; given a start and a destination, return whether the ball can stop exactly at the destination.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


In a maze grid of 0 (open) and 1 (wall), a ball rolls in one of four directions and keeps going until it hits a wall; given a start and a destination, return whether the ball can stop exactly at the destination.

A Google onsite graph-modeling problem: the key twist is that nodes are stopping points, not every cell. The interviewer checks that you redefine the neighbors correctly and do not mark cells you only roll through as visited.

What the Interviewer Expects
  • Models states as stopping cells only
  • Generates neighbors by rolling until the next cell is a wall or out of bounds
  • Uses BFS or DFS with a visited set on stopping cells
  • Recognizes that passing over the destination does not count
  • States O(mn(m + n)) time in the worst case
Key Topics to Cover
BFS
Implicit Graphs
Grid Simulation
State Modeling
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Return the shortest distance in cells rolled (weighted edges).
  • Return the lexicographically smallest direction string to drop into a hole that the ball can pass over.
  • Precompute roll destinations to make each neighbor lookup O(1).
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • maze m x n of 0/1, start and dest are open cells.
  • The ball stops only when the next cell is a wall or the boundary.
  • Output: True if dest can be a stopping point....
High-Level Design

Each stopping cell is a node. From a node, simulate rolling in each direction until the next step is blocked; the final cell is a neighbor. BFS over these nodes. Rolling through the destination withou...


Submit Your Answer
Markdown supported