Find the exit column of each ball dropped through a grid of diagonal boards

Quick Overview

Simulate balls dropped through a grid of diagonal boards and report the column each one falls out of, or -1 when it gets stuck against a wall or in a V between two boards. It tests careful grid simulation, bounds checks and neighbor checks within a row.

Find the exit column of each ball dropped through a grid of diagonal boards

Company: Waymo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You have a 2-D `grid` of size `m x n` representing a box, and you have `n` balls. The box is open on the top and bottom sides. Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left. - A board that redirects the ball to the right spans the top-left corner to the bottom-right corner (drawn `\`) and is represented in the grid as `1`. - A board that redirects the ball to the left spans the top-right corner to the bottom-left corner (drawn `/`) and is represented in the grid as `-1`. One ball is dropped at the top of each column of the box. Each ball can get stuck in the box or fall out of the bottom. A ball gets stuck if it hits a V-shaped pattern between two boards or if a board redirects the ball into either wall of the box. Return a list `answer` of length `n` where `answer[i]` is the column that the ball dropped into column `i` falls out of at the bottom, or `-1` if that ball gets stuck in the box. ### Function Signature ```python def find_ball(grid: list[list[int]]) -> list[int]: ``` ### Rules - Rows are numbered from the top (row 0) and columns from the left (column 0). Each ball is dropped on its own, so balls never block each other. - A ball in row `r`, column `c` with `grid[r][c] == 1` leaves row `r` in column `c + 1`. It is stuck instead if `c + 1 == n` (the right wall) or if `grid[r][c + 1] == -1` (the two boards form a V). - A ball in row `r`, column `c` with `grid[r][c] == -1` leaves row `r` in column `c - 1`. It is stuck instead if `c - 1 < 0` (the left wall) or if `grid[r][c - 1] == 1`. - A ball that gets through the last row falls out of the bottom in the column it is in. ### Constraints - `m == len(grid)` - `n == len(grid[i])` - `1 <= m, n <= 100` - `grid[i][j]` is `1` or `-1`. ### Examples ```text Input: grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]] Output: [1,-1,-1,-1,-1] ``` Ball 0 is in columns 1, 2, 1, 2, 1 after rows 0 to 4 and falls out of column 1. Balls 2 and 3 meet the V between columns 2 and 3 in row 0, while balls 1 and 4 each move one column toward the middle in row 0 and meet the V between columns 2 and 3 in row 1. ```text Input: grid = [[-1]] Output: [-1] ``` The only board sends the ball to the left, straight into the left wall. ```text Input: grid = [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]] Output: [0,1,2,3,4,-1] ``` Balls 0 to 4 move one column right in rows 0 and 2 and one column left in rows 1 and 3, so each one falls out of the column it started in. Ball 5 is sent into the right wall in row 0.

Overview: Simulate balls dropped through a grid of diagonal boards and report the column each one falls out of, or -1 when it gets stuck against a wall or in a V between two boards. It tests careful grid simulation, bounds checks and neighbor checks within a row.

Waymo logo
Waymo
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You have a 2-D grid of size m x n representing a box, and you have n balls. The box is open on the top and bottom sides.

Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.

  • A board that redirects the ball to the right spans the top-left corner to the bottom-right corner (drawn \ ) and is represented in the grid as 1 .
  • A board that redirects the ball to the left spans the top-right corner to the bottom-left corner (drawn / ) and is represented in the grid as -1 .

One ball is dropped at the top of each column of the box. Each ball can get stuck in the box or fall out of the bottom. A ball gets stuck if it hits a V-shaped pattern between two boards or if a board redirects the ball into either wall of the box.

Return a list answer of length n where answer[i] is the column that the ball dropped into column i falls out of at the bottom, or -1 if that ball gets stuck in the box.

Function Signature

def find_ball(grid: list[list[int]]) -> list[int]:

Rules

  • Rows are numbered from the top (row 0) and columns from the left (column 0). Each ball is dropped on its own, so balls never block each other.
  • A ball in row r , column c with grid[r][c] == 1 leaves row r in column c + 1 . It is stuck instead if c + 1 == n (the right wall) or if grid[r][c + 1] == -1 (the two boards form a V).
  • A ball in row r , column c with grid[r][c] == -1 leaves row r in column c - 1 . It is stuck instead if c - 1 < 0 (the left wall) or if grid[r][c - 1] == 1 .
  • A ball that gets through the last row falls out of the bottom in the column it is in.

Constraints

  • m == len(grid)
  • n == len(grid[i])
  • 1 <= m, n <= 100
  • grid[i][j] is 1 or -1 .

Examples

Input: grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]
Output: [1,-1,-1,-1,-1]

Ball 0 is in columns 1, 2, 1, 2, 1 after rows 0 to 4 and falls out of column 1. Balls 2 and 3 meet the V between columns 2 and 3 in row 0, while balls 1 and 4 each move one column toward the middle in row 0 and meet the V between columns 2 and 3 in row 1.

Input: grid = [[-1]]
Output: [-1]

The only board sends the ball to the left, straight into the left wall.

Input: grid = [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]
Output: [0,1,2,3,4,-1]

Balls 0 to 4 move one column right in rows 0 and 2 and one column left in rows 1 and 3, so each one falls out of the column it started in. Ball 5 is sent into the right wall in row 0.