FastPrepBubble Popping Game

Bubble Popping Game

Instacart logoInstacartâ—Ź MediumFULLTIMEOA
Learn

Problem statement

You are given a 2D array grid, where grid[i][j] represents the color of the bubble at position (i, j). You are also given an operations array, which is a sequence of coordinates indicating where to pop bubbles.

Rules:

  • If you pop a bubble at a position with value 0 (meaning no bubble), nothing happens.
  • If you pop a bubble, check its top-left, bottom-left, top-right, and bottom-right diagonal neighbors:
  • If the neighbor is within bounds, has a bubble (non-zero), and the color is the same as the popped bubble, that neighbor also pops.
  • After popping, gravity applies: bubbles above empty cells fall down to fill the empty spaces directly below them.
  • Perform all operations in order and return the final grid.

Function

popBubbles(grid: int[][], operations: int[][]) → int[][]

Examples

Example 1

grid = [[1, 2, 3], [6, 1, 2], [1, 2, 5]]operations = [[1, 1], [1, 2]]return = [[0, 0, 0], [0, 0, 3], [6, 2, 5]]

Step 1: Pop bubble at (1, 1)

  • Pops the bubble itself
  • Pops diagonally connected bubble (0, 0) because it has the same color 1

Apply gravity:

        [
          [0, 0, 3],
          [0, 2, 2],
          [6, 2, 5]
        ]
        

Step 2: Pop bubble at (1, 2)

  • Pops the bubble itself
  • Pops diagonal (0, 1) because it's 2
  • Pops diagonal (2, 1) because it's also 2

Apply gravity:

        [
          [0, 0, 0],
          [0, 0, 3],
          [6, 2, 5]
        ]
        

Final Output:

        [
          [0, 0, 0],
          [0, 0, 3],
          [6, 2, 5]
        ]
        

More Instacart problems

See Instacart hiring insights
public int[][] popBubbles(int[][] grid, int[][] operations) {
  // write your code here
}
grid[[1, 2, 3], [6, 1, 2], [1, 2, 5]]
operations[[1, 1], [1, 2]]
expected[[0, 0, 0], [0, 0, 3], [6, 2, 5]]
Checking account…