Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Bounding Box From 2D Array

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Bounding Box From 2D Array. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

A Reddit image preview is represented by a non-empty 2D array containing only 1s and 0s. The 1s represent background pixels, while the 0s form one solid, axis-aligned rectangular region. Return the coordinates of that region's upper-left and bottom-right corners.

Formal Specification

Implement find_bounding_box(image), where image is a rectangular list of lists of integers. Return [[top_row, left_column], [bottom_row, right_column]]. Coordinates are zero-based, and both corners are inclusive. The input contains at least one 0, and every 0 belongs to the same rectangular region.

Constraints

  • 1 <= len(image) <= 10^4
  • 1 <= len(image[0]) <= 10^4
  • The matrix is rectangular
  • Each cell is either 0 or 1
  • At least one cell is 0
  • All 0s form one solid axis-aligned rectangle

Function Signature

def find_bounding_box(image):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output