Count Disappearing Connected Tiles
Updated
Problem statement
A player selects the tile at [row,col] in a rectangular integer grid. The selected tile disappears, together with every same-valued tile you can reach from it by moving up, down, left or right through same-valued tiles. Tiles are not connected diagonally.
Return how many tiles disappear, including the selected tile. Other components with the same number are not counted; tiles with different values stop connectivity.
For this exercise, assume a nonempty equal-width board with 1 to 30 rows and columns, integer digits 0 through 9, and a valid zero-based selection.
Function
disappear(grid: int[][], row: int, col: int) → intExamples
Example 1
grid = [[4,4,4,4],[5,5,5,4],[2,5,7,5]]row = 0col = 0return = 5This exact source testcase removes 5 equal-valued tiles from the selected four-neighbor component.
Example 2
grid = [[4,4,4,4],[5,5,5,4],[2,5,7,5]]row = 1col = 1return = 4This exact source testcase removes 4 equal-valued tiles from the selected four-neighbor component.
Example 3
grid = [[4,4,4,4],[5,5,5,4],[2,5,7,5]]row = 1col = 0return = 4This exact source testcase removes 4 equal-valued tiles from the selected four-neighbor component.
Example 4
grid = [[0,3,3,3,3,3,3],[0,1,1,1,1,1,3],[0,2,2,0,2,1,4],[0,1,2,2,2,1,3],[0,1,1,1,1,1,3],[0,0,0,0,0,0,0]]row = 0col = 0return = 12This exact source testcase removes 12 equal-valued tiles from the selected four-neighbor component.
Example 5
grid = [[0,3,3,3,3,3,3],[0,1,1,1,1,1,3],[0,2,2,0,2,1,4],[0,1,2,2,2,1,3],[0,1,1,1,1,1,3],[0,0,0,0,0,0,0]]row = 3col = 0return = 12This exact source testcase removes 12 equal-valued tiles from the selected four-neighbor component.
Example 6
grid = [[0,3,3,3,3,3,3],[0,1,1,1,1,1,3],[0,2,2,0,2,1,4],[0,1,2,2,2,1,3],[0,1,1,1,1,1,3],[0,0,0,0,0,0,0]]row = 1col = 1return = 13This exact source testcase removes 13 equal-valued tiles from the selected four-neighbor component.
Example 7
grid = [[0,3,3,3,3,3,3],[0,1,1,1,1,1,3],[0,2,2,0,2,1,4],[0,1,2,2,2,1,3],[0,1,1,1,1,1,3],[0,0,0,0,0,0,0]]row = 2col = 2return = 6This exact source testcase removes 6 equal-valued tiles from the selected four-neighbor component.
Example 8
grid = [[0,3,3,3,3,3,3],[0,1,1,1,1,1,3],[0,2,2,0,2,1,4],[0,1,2,2,2,1,3],[0,1,1,1,1,1,3],[0,0,0,0,0,0,0]]row = 0col = 3return = 7This exact source testcase removes 7 equal-valued tiles from the selected four-neighbor component.
Example 9
grid = [[0]]row = 0col = 0return = 1This exact source testcase removes 1 equal-valued tiles from the selected four-neighbor component.
Example 10
grid = [[1,1,1],[1,1,1],[1,1,1]]row = 0col = 0return = 9This exact source testcase removes 9 equal-valued tiles from the selected four-neighbor component.
Constraints
- For this exercise, assume
1 <= grid.length, grid[0].length <=30and every row has the same length. 0 <= grid[r][c] <=9.0 <= row < grid.lengthand0 <= col < grid[0].length.
More Indeed Phone Screen questions
- Insert into an Unrolled Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Course Completion OrderPHONE SCREEN · Seen May 2025
- Recipes You Can PreparePHONE SCREEN · Seen May 2025
- Rotting Orange PropagationPHONE SCREEN · Seen May 2025
- Courses Shared by Every Student PairPHONE SCREEN · Seen Aug 2022
- First and Last Access Time per UserPHONE SCREEN · Seen Jul 2022
- Most Accessed Resource in Five MinutesPHONE SCREEN · Seen Jul 2022
- Resource Transition GraphPHONE SCREEN · Seen Jul 2022
- First Dictionary Word from Note LettersPHONE SCREEN · Seen Jul 2022
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Jul 2022
Similar questions at other companies
- Matrix EscapeByteDance · PHONE SCREENByteDance interview questions
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Reachable Exits in an Obstacle GridWhatNot · PHONE SCREENWhatNot interview questions
- Process Minesweeper Game OperationsAmperity · PHONE SCREENAmperity interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- Fuel-Aware Grid ReachabilityBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Bounds in a Ragged GridRippling · OA · PHONE SCREENRippling interview questions