FastPrepBounds of Every Zero Rectangle
Problems/Bounds of Every Zero Rectangle

Bounds of Every Zero Rectangle

Indeed logoIndeed● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Indeed phone screen. The task is to find every rectangle of 0s inside an image of 1s.

The report accepts two output forms, two corners or one corner with its width and height, and does not say how to order the rectangles or how big the image can be. Here, each rectangle is one row [top,left,bottom,right], rows are ordered by the top-left corner (row, then column), two rectangles never share an edge, and the image is at most 30 by 30. Examples 4 and 5 are this report's own samples. Example 1's matrix comes from a different report that gives only the matrix and a count of four rectangles.

Our version is about 95% the same as the reported question.

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]]
matrix
01234001111111001201001301111410111
→
return
012300000112232203034141

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]]
matrix
012000110012110
→
return
01230001112222

This example contains two diagonally touching filled rectangles.

Example 3

matrix = [[1,1],[1,1]]return = []
matrix
01011111

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]]
matrix
01234560011111111111111201100013101000141011111510100116111001171111110
→
return
0123000001202022335331514536457676

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]]
matrix
01234011111110001210001310001411111
→
return
012301133

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]]
matrix
012001010112010
→
return
0123000201020222222

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 1 through 30, and all rows have the same length.
  • Every cell is the integer 0 or 1.
  • 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

Interview reports that mention this question

  • Indeed Experienced SWE Karat Screens and OAs: 6 Experiences

Indeed interview experiences

  • Indeed SWE Intern Karat Screens and OA: 3 Experiences
  • Indeed New Grad SWE Online Assessments: 3 Experiences
All Indeed interview questionsSee Indeed hiring insights
Saved
class Solution {
    public int[][] allRectangleBounds(int[][] matrix) {
        // Write your code here.
    }
}
Checking account…