The "Smallest Rectangle Enclosing Black Pixels" interview question is a 2D grid problem. You are given a binary matrix where '1' represents black pixels and '0' represents white pixels. All black pixels are connected. Given the coordinates of one black pixel, you need to find the area of the smallest axis-aligned rectangle that encloses all black pixels. This "Smallest Rectangle Enclosing Black Pixels coding problem" is about finding the boundaries (top, bottom, left, right) of the black pixel cluster.
Google asks this to test a candidate's ability to optimize search in a grid. While a simple BFS or DFS starting from the given pixel can find all black pixels and their boundaries in O(B) time (where B is the number of black pixels), there is a more efficient O(M log N + N log M) solution using binary search. It evaluates your ability to apply 1D search techniques (binary search) to 2D projections.
This problem follows the "Binary Search and Matrix interview pattern".
0 to y. For each column, you check if it contains any '1' by scanning that column.(right - left + 1) * (bottom - top + 1).Grid:
0000
0110
0100
Given pixel: (1, 1)
(2-1+1) * (2-1+1) = 4.The most common mistake is using BFS/DFS when a faster binary search approach is possible. Another error is not correctly setting the ranges for the four binary searches. Some candidates also fail to realize that the "connected" constraint is what makes binary search applicable in the first place.
Whenever you have a connected component in a grid and need to find its "extremes," consider whether you can binary search on the row and column projections. This "Smallest Rectangle Enclosing Black Pixels interview question" is the classic example of this optimization.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Last Day Where You Can Still Cross | Hard | Solve | |
| Escape the Spreading Fire | Hard | Solve | |
| Find All Groups of Farmland | Medium | Solve | |
| Coloring A Border | Medium | Solve | |
| Minesweeper | Medium | Solve |