Problem
Ask
Submissions

Problem: Where Will the Ball Fall

Medium
30 min
Understand how to simulate the path of balls dropped into a 2D grid with diagonal redirects, determining their exit points or if they get stuck. Learn to analyze grid patterns that influence ball movement, handle boundary conditions, and apply efficient traversal methods. This lesson helps you develop problem-solving skills essential for matrix-based coding interview questions.

Statement

You have nn balls and a 2D grid of size m×nm \times n representing a box. The box is open on the top and bottom sides. Each cell in the box has a diagonal that can redirect a ball to the right or the left. You must drop nn balls at each column’s top. The goal is to determine whether each ball will fall out of the bottom or become stuck in the box. Each cell in the grid has a value of 11 or 1-1.

  • 11 represents that the grid will redirect the ball to the right.
  • 1-1 represents that the grid will redirect the ball to the left.

A ball gets stuck if it hits a V-shaped pattern between two grids or if a grid redirects the ball into either wall of the box.

The solution should return an array of size nn, with the ithith element indicating the column that the ball falls out of, or it becomes 1-1 if it’s stuck. If the ball drops from column xx and falls out from column yy, then in the result array, index xx contains value yy.

Constraints:

  • m==m == grid.length
  • n==n == grid[i].length
  • 1m,n1001 \leq m, n \leq 100
  • grid[i][j] is 11 or 1-1.
Problem
Ask
Submissions

Problem: Where Will the Ball Fall

Medium
30 min
Understand how to simulate the path of balls dropped into a 2D grid with diagonal redirects, determining their exit points or if they get stuck. Learn to analyze grid patterns that influence ball movement, handle boundary conditions, and apply efficient traversal methods. This lesson helps you develop problem-solving skills essential for matrix-based coding interview questions.

Statement

You have nn balls and a 2D grid of size m×nm \times n representing a box. The box is open on the top and bottom sides. Each cell in the box has a diagonal that can redirect a ball to the right or the left. You must drop nn balls at each column’s top. The goal is to determine whether each ball will fall out of the bottom or become stuck in the box. Each cell in the grid has a value of 11 or 1-1.

  • 11 represents that the grid will redirect the ball to the right.
  • 1-1 represents that the grid will redirect the ball to the left.

A ball gets stuck if it hits a V-shaped pattern between two grids or if a grid redirects the ball into either wall of the box.

The solution should return an array of size nn, with the ithith element indicating the column that the ball falls out of, or it becomes 1-1 if it’s stuck. If the ball drops from column xx and falls out from column yy, then in the result array, index xx contains value yy.

Constraints:

  • m==m == grid.length
  • n==n == grid[i].length
  • 1m,n1001 \leq m, n \leq 100
  • grid[i][j] is 11 or 1-1.