Bounds of Every Zero Rectangle
Updated
Problem statement
Given a nonempty rectangular integer matrix matrix containing only 0 and 1, return the bounds of every zero rectangle.
For this exercise, assume every group of zeros connected through immediately adjacent up, down, left or right cells is a filled rectangle whose sides follow the matrix’s rows and columns. Separate rectangles never touch along an edge; they may touch diagonally. There may be no zero rectangle. The input will not contain other shapes or require splitting overlapping rectangles.
For this exercise, assume coordinates are zero-based and both endpoints are inclusive. Return one row [top,left,bottom,right] per rectangle, ordered by its top-left row and then column. The four integers encode the two corner coordinates. An input with no zeros returns an empty matrix.
Function
allRectangleBounds(matrix: int[][]) → int[][]Examples
Example 1
matrix = [[0,1,1,1,1],[1,1,0,0,1],[0,1,0,0,1],[0,1,1,1,1],[1,0,1,1,1]]return = [[0,0,0,0],[1,2,2,3],[2,0,3,0],[4,1,4,1]]This matrix has four rectangles; their bounds follow from the stated rules.
Example 2
matrix = [[0,0,1],[0,0,1],[1,1,0]]return = [[0,0,1,1],[2,2,2,2]]This example contains two diagonally touching filled rectangles.
Example 3
matrix = [[1,1],[1,1]]return = []This example contains no zero rectangle.
Example 4
matrix = [[0,1,1,1,1,1,1],[1,1,1,1,1,1,1],[0,1,1,0,0,0,1],[1,0,1,0,0,0,1],[1,0,1,1,1,1,1],[1,0,1,0,0,1,1],[1,1,1,0,0,1,1],[1,1,1,1,1,1,0]]return = [[0,0,0,0],[2,0,2,0],[2,3,3,5],[3,1,5,1],[5,3,6,4],[7,6,7,6]]This matrix has six rectangles. Three of the rectangles are single cells. The zeros at [2,0] and [3,1] touch only at a corner, so they are two separate rectangles.
Example 5
matrix = [[1,1,1,1,1],[1,0,0,0,1],[1,0,0,0,1],[1,0,0,0,1],[1,1,1,1,1]]return = [[1,1,3,3]]This sample has one 3 by 3 block of zeros inside a border of ones.
Example 6
matrix = [[0,1,0],[0,1,1],[0,1,0]]return = [[0,0,2,0],[0,2,0,2],[2,2,2,2]]The rectangle in column 0 ends in row 2, but it starts in row 0, so it comes before the single cell at [0,2]. Rows follow each rectangle's top-left corner, not its bottom-right corner.
Constraints
- For this exercise, assume row and column counts are each from
1through30, and all rows have the same length. - Every cell is the integer
0or1. - Each orthogonally connected zero component fills its bounding rectangle.
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
- Count Disappearing Connected TilesPHONE SCREEN · Seen Jul 2022
- First Dictionary Word from Note LettersPHONE 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
- Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Reachable Exits in an Obstacle GridWhatNot · PHONE SCREENWhatNot interview questions
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Find Robots by Blocker DistancesUber · PHONE SCREENUber interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN