Bubble Popping Game
Updated
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]]Moved or new
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 interview questions
- Find First and Last Position in a Sorted ArrayPHONE SCREEN · Seen Nov 2026
- Squares of a Sorted ArrayPHONE SCREEN · Seen Nov 2026
- Evaluate Formulas with Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Process Shopping Records, Promotions, and Display OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Product Total With the Cheapest PromotionONSITE INTERVIEW · Seen Sep 2026
- Sort Products by Aisle With Frozen Items LastONSITE INTERVIEW · Seen Sep 2026
- Timed Task ManagementONSITE INTERVIEW · Seen Sep 2026
- Worker Management with Promotions and Double-Paid TimeONSITE INTERVIEW · Seen Sep 2026
- 🦉 Collect SticksOA · Seen Mar 2025
- Balloon Color PairsOA · Seen Mar 2025
Similar questions at other companies
- Simulate Orchard Rot SpreadAirbnb · OAAirbnb interview questions
- Simulate Orchard Rot SpreadCapital One · OACapital One interview questions
- Rotate Matrix Regions Around the DiagonalsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Rotate Matrix Regions Around the DiagonalsHudson River Trading · OAHudson River Trading interview questions
- Robot Navigation Around LasersRoblox · OARoblox interview questions
- Leaves Remaining After Wind GustsOpenAI · OAOpenAI interview questions
- Apply a Reversi MoveAmazon · OA
- Flip, Invert, and Smooth a Binary ImageAbridge · OAAbridge interview questions
- Count Straight-Line Word Occurrences in a GridOptiver · OAOptiver interview questions
- First Completed Mingo LineEpic · OAEpic interview questions