Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Nearest Edge Distance Map

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

Your question is Nearest Edge Distance Map. 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

Given a binary matrix image of size m x n where 0 represents background and 1 represents foreground, return an m x n matrix dist where dist[r][c] is the minimum 4-directional distance from cell (r, c) to any background cell. Distances are measured using up, down, left, and right moves. If a cell is already background, its distance is 0.

Constraints

  • 1 <= m, n <= 200
  • image[i][j] is either 0 or 1
  • At least one background cell exists in the matrix

Function Signature

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