Where Each Marble Exits a Grid of Diagonal Deflectors
Last updated: November 29, 2026
Quick Overview
A grid of deflectors has cells with 1 (a board from top-left to bottom-right, sending marbles right) or -1 (top-right to bottom-left, sending marbles left). Drop one marble at the top of each column and return the column where it exits the bottom, or -1 if it gets stuck against a wall or in a V shape.
0
0
0 solved
A grid of deflectors has cells with 1 (a board from top-left to bottom-right, sending marbles right) or -1 (top-right to bottom-left, sending marbles left). Drop one marble at the top of each column and return the column where it exits the bottom, or -1 if it gets stuck against a wall or in a V shape.
An online assessment style simulation Google uses to check careful per-step rules. The interviewer wants the stuck conditions stated clearly, a row-by-row trace for each marble, and a complexity argument.
What the Interviewer Expects
- State the movement rule: each row, the marble moves to column c + grid[r][c]
- Detect getting stuck at a wall (next column out of bounds)
- Detect a V shape (neighboring cell points the opposite way)
- Simulate each column independently
- Give O(m * n) time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Could you compute all answers with a single bottom-up pass?
- What if the grid wraps around horizontally?
- How would you handle a third cell type that passes marbles straight down?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: an m x n grid of 1 and -1. A marble in column c at row r moves to column c + grid[r][c] as it descends to row r + 1. It is stuck if that column is outside the grid, or if `grid[r][nc] !...
High-Level Design
For each starting column, loop over rows: compute d = grid[r][c], nc = c + d. If nc is out of range or grid[r][nc] != d, the marble is stuck. Otherwise set c = nc. After all rows, the marble...